我正试图通过名字访问一个特定的子元素.现在,因为子元素在哪里,我用这个称呼子元素:

this.$root.$children[0]

只要子元素总是[0]岁就可以了,但如果有一种方法可以做到:

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

我一直在想$refs可能是我问题的答案,但却永远找不到一种方法来帮助我.

有什么 idea 吗?

推荐答案

你说的这个子元素真的是你想从中访问它的组件的子元素吗?在这种情况下,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

Vue.js相关问答推荐

当props 的值改变时如何更新类'

Visual Studio代码中的VUE/VLAR扩展不断崩溃:JS/TS语言服务立即崩溃5次…

Vue 3组合式API如何测试是否发出正确的事件

Vue.js 3 运行时挂载组件实例

vue js如何将布尔值传到数据库

Axios Intercept 在第一次调用时未从 localStorage 应用令牌

如何在 vue.js 模板中显示空间而不是 undefined 和 null?

带有外部配置文件的 Vue js

Vue:限制文本区域输入中的字符,截断过滤器?

带有子菜单的 Vuetify 导航抽屉

Vuejs模板继承

当 v-for 与对象的属性一起使用时,将 v-model 与复选框一起使用

阻止 Vue 积极重用 dom 元素

为什么 v-model 不适用于数组和 v-for 循环?

为什么 Vue.js 使用 VDOM?

'不提供名为'createRouter'的导出'' vue 3、vite 和 vue-router

执行调度程序刷新期间未处理的错误,这可能是一个 Vue 内部错误

如何从组件内的单点捕获 vuejs 错误

脚本npm run dev和npm run watch是做什么用的?

如何在 vue3 中的setup方法中发出事件?