Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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] but it would be great if there’s a way to do something like:

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

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

Any ideas?

like image 324
Flakx Avatar asked Mar 03 '16 10:03

Flakx


2 Answers

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

like image 72
Linus Borg Avatar answered Oct 01 '22 10:10

Linus Borg


You can use this property:

this.$root.$children[0].$options.name 

For example:

this.$root.$children.find(child => { return child.$options.name === "name"; }); 
like image 45
drinor Avatar answered Oct 01 '22 10:10

drinor