我正在尝试按名称访问特定的 child 。目前,由于 child 所在的位置,我是这样称呼 child 的:
this.$root.$children[0]
只要那个 child 总是 [0]
就没问题,但如果有办法做这样的事情就太好了:
this.$root.$children['detail']
我一直在想 $refs
可能是我的问题的答案,但永远找不到对我有帮助的方法。
有什么想法吗?
最佳答案
您所说的这个子组件真的是您要从中访问它的组件的子组件吗?在这种情况下,v-ref
确实是答案:
// 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
关于javascript - Vue.js $children 按组件名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35769139/