admin管理员组

文章数量:1287624

Problem
I'm working with a Vue CLI application. There is a grandchild ponent that needs access to another ponent's element's properties. It really only needs the clientHeight property.

So Content2.vue ponent needs access to the clientHeight property of this element: <div id="header"></div> (in Header.vue).

Attempted Solutions

  • I've tried using $refs. And according to this stackoverflow answer $refs are only available to ponents directly related as parent/child.

  • I've also considered adding the $refs or clientHeight to the store (vuex), but its seems like overkill.

  • I could always go vanilla and use querySelector or getElementById. But thought there may be something better.

Thanks for your help!

Problem
I'm working with a Vue CLI application. There is a grandchild ponent that needs access to another ponent's element's properties. It really only needs the clientHeight property.

So Content2.vue ponent needs access to the clientHeight property of this element: <div id="header"></div> (in Header.vue).

Attempted Solutions

  • I've tried using $refs. And according to this stackoverflow answer $refs are only available to ponents directly related as parent/child.

  • I've also considered adding the $refs or clientHeight to the store (vuex), but its seems like overkill.

  • I could always go vanilla and use querySelector or getElementById. But thought there may be something better.

Thanks for your help!

Share Improve this question asked Oct 1, 2019 at 22:29 RyanRyan 5706 silver badges20 bronze badges 2
  • It seems like you are aware of all the possible solutions. My personal remendation would be to use vuex. You will get a lot of mileage out of learning and familiarizing yourself with vuex. It might seem like overkill for this particular use case, but I think a simple case like this might be the best way to learn how to use it. The added bonus is, then it's already in place for the next time you need to reach for it.

    本文标签: javascriptHow to access Vue component element property from sibling componentStack Overflow