Vue.js $children 按组件名称 [英] Vue.js $children by component name
本文介绍了Vue.js $children 按组件名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试按名称访问特定的孩子.此刻,因为孩子在哪里,我这样称呼孩子:
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]
只要那个孩子总是 [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']
我一直认为 $refs
可能是我问题的答案,但永远找不到对我有帮助的方法.
I keep thinking $refs
might be the answer to my problem but can never find a way that it helps me.
有什么想法吗?
推荐答案
您所说的这个孩子真的是您想要访问它的组件的孩子吗?在这种情况下,v-ref
确实是答案:
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>
相关 API 文档:http://vuejs.org/api/#v-ref
这篇关于Vue.js $children 按组件名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文