如何访问在组件的命名槽内呈现的 Vue 组件的功能?
我有以下结构:
父组件.vue
...
<util-component>
<template v-slot:0>
<child-component/>
</template>
<template v-slot:1>
//Another component, and so on
</template>
</util-component>
...
实用组件.vue
...
<div v-for="(comp, index) in components" :key="index">
<slot :name="index" ></slot>
</div>
...
子组件.vue
...
methods: {
myFunction () { // do something }
}
...
在UtilComponent
中,我想访问ChildComponent
's myFunction()
(单击按钮时,不在安装时)。我曾尝试this.$slots[0]
在 UtilComponent 中使用,但它只给我一个VNode
没有我需要的属性(数据和函数)。this.$slots[0].context
给了我完整的 ParentComponent。有了this.$slots[0][0].context.$children[0].$children[1]
我实际上可以访问 ChildComponent 及其功能,但这似乎很迂回(获取渲染插槽的父级然后获取该父级的子级的子级......)
有没有更好的方法ChildComponent
从提供该插槽(此处,)的组件访问在插槽(此处,)内呈现的组件(的功能UtilComponent
)?