html中
<el-tree :data="list" :props="defaultProps" @node-click="handleNodeClick"></el-tree>
data中注册
list: [{
id: 1,
label: '一级 1',
children: [{
id: 2,
label: '二级 1-1',
children: [{
id: 3,
label: '三级 1-1-1'
}, {
id: 4,
label: '三级 1-1-2'
}, {
id: 5,
label: '三级 1-1-3'
}]
}]
}, {
id: 6,
label: '一级 2',
children: [{
id: 7,
label: '二级 2-1'
}, {
id: 8,
label: '二级 2-2'
}]
}],
defaultProps: {
children: 'children',
label: 'label'
},
childArr: []
methods中
handleNodeClick (data) {
console.log('data: ', data)
this.childArr = []
this.getAllChild(data)
console.log(this.childArr)
},
getAllChild (obj) {
if (Object.prototype.hasOwnProperty.call(obj, 'children')) {
for (const item of obj.children) {
if (Object.prototype.hasOwnProperty.call(item, 'children')) {
this.childArr.push(item.id)
this.getAllChild(item)
} else {
this.childArr.push(item.id)
}
}
this.getAllChild(obj.children)
} else {
return null
}
}