HTML
<el-tree
:data="data"
show-checkbox
node-key="id"
:default-expanded-keys="defaultExpandedKeys"
@node-expand="handleNodeExpand"
@node-collapse="handleNodeCollapse">
</el-tree>
JS
<script>
export default {
data() {
return {
// 树集合
data: [
{
id: 1,
label: '一级 2',
children: [{
id: 3,
label: '二级 2-1',
children: [{
id: 4,
label: '三级 3-1-1'
}, {
id: 5,
label: '三级 3-1-2',
disabled: true
}]
}
],
// 默认展开的节点的 key 的数组
defaultExpandedKeys: []
}
},
methods: {
// 当节点展开时记录节点id
handleNodeExpand(data, node) {
this.defaultExpandedKeys.push(data.id);
},
// 当节点收缩时删除节点id
handleNodeCollapse(data, node){
this.defaultExpandedKeys = this.defaultExpandedKeys.filter(item => item !== data.id)
}
}
</script>