父组件
async getChildComponentInstances() {
for (let i = 0; i < this.newshowTree.length; i++) {
const value = this.newshowTree[i]
// console.log(value)
if (i == 0) {
this.traverse(this.$refs.rootComponent)
//在这里traverse函数是获取根组件的子组件实例
// traverse(component) {
// if (component !== undefined) {
// this.childComponentInstances.push(component)
// }
// },
}
//当前的组件下的子组件的vue实例
let treeSon = this.childComponentInstances
for (let j = 0; j < treeSon.length; j++) {
treeSon[j].isOpen = false
if ('model' in treeSon[j]) {
if (value == treeSon[j].model.id) {
//调用我们需要找到的哪个组件的autoExpand()函数
await treeSon[j].autoExpand()
if (treeSon[j].$children.length > 0) {
this.childComponentInstances = []
//获取到这个组件下的子组件实例
treeSon[j].$children.forEach((child) => {
this.childComponentInstances.push(child)
})
// console.log(this.childComponentInstances)
}
}
}
}
}
},
子组件如下,是个只包含自己的递归组件
<template>
<li>
<ul class="de-animate-wrap" v-show="isOpen">
<TreeItem v-for="(item, index) in childNode" :key="index" class="item" :model="item" />
</ul>
</li>
</template>