<el-tree
ref="tree"
node-key="id" --必须定义
:data="treeData"
highlight-current --高亮显示当前行
default-expand-all
:props="defaultProps">
</el-tree>
接下来的步骤分为两种情况:
一、数据treeData直接在data里面定义死了
fetchTreeData(){
this.$refs.tree.setCurrentKey('你想要高亮显示的那个节点的ID');
}
二、数据treeData需要访问接口得到
此时 this. $ refs.tree.setCurrentKey 方法必须写在this.$nextTick(function(){}里面。因为请求接口和设置当前行方法会产生异步问题,导致设置当前行失败
fetchTreeData() {
ykService_getYhxxTreeInYk(xxxxx).then(res => {
this.treeData= res.content; //动态给树赋值
this.$nextTick(function(){
this.$refs.tree2.setCurrentKey('你想要高亮显示的那个节点的ID');
//例如 this.$refs.tree2.setCurrentKey(res.content[0].children[0].id);
})
})
}
然后直接在mounted钩子函数里面调用fetchTreeData() 方法