Flakx Flakx - 2 months ago 80
Javascript Question

Vue.js $children by component name

i'm trying to access a specific child by name. At the moment because of where the child is, i'm calling the child by this:

this.$root.$children[0]


which is ok as long as that child is always [0] by ideally it would be great if theres a way to do something like:

this.$root.$children['detail']


i keep thinking $refs might be the anwser to my problem but can never find a way that it helps me.

any ideas?

Answer

Is this child you are talking about really a child of the component that you want to access it from? In this case, v-ref is indeed the answer:

// in the code of the parent component, access the referenced child component like this:

this.$refs.detailsChild
<!-- Template of the parent component, assuming your child Component is called Details -->
<details v-ref:details-child></details>

relevant API Documentation: http://vuejs.org/api/#v-ref

Comments