Using v2.2.2. I have the following structure in my VueJS app. How can I access all of the Account components from a method on my Post component? The number of Accounts is dynamic - I want to grab all of the loaded Account
components and iterate over them.
I know it's got something to do with $refs
and $children
, I just can't seem to figure out the right path.
<Root>
<Post>
<AccountSelector>
<Account ref="anAccount"></Account>
<Account ref="anAccount"></Account>
<Account ref="anAccount"></Account>
</AccountSelector>
</Post>
</Root>
http://vuejs.org/v2/guide/components.html#Child-Component-Refs
Basically inside
AccountSelector.vue
you can dothis.$refs.anAccount.map(account => doThingToAccount(account))
Edit The above answer is for accessing a direct child.
Currently there is no way to access a non direct parent / child component with
$refs
. The best way to get access to their data would be through events http://vuejs.org/v2/guide/components.html#Non-Parent-Child-Communication or by using Vuex (Which i would recommend).Non direct parent - child communication is very tricky without 1 of these 2 methods.
You can access nested components data using $refs and if you want to access the refs inside of it you first have to target the first element of the first refs ([0])
example:
parent.$refs[0].$refs.anAccount