vue elementUI el-tree默认选中树节点效果
vue elementUI el-tree默认选中树节点
1.设置一个固定值作为key:node-key="id"
2.定义当前选中节点的key::current-node-key="currentId"
<el-tree node-key="id" :current-node-key="currentId" :data="treeData" :props="defaultProps" :check-on-click-node="true" :accordion="true" empty-text="组织机构" icon-class="" :filter-node-method="filterNode" ref="tree" :expand-on-click-node="false" @node-click="handleNodeClick"></el-tree>
3.将当前选中的key设置到el-tree中:this.currentId
getTreeList().then((res) => { const data = res.data.data this.treeData = data
if (data.length > 0) { this.currentId = data[0].id
this.$nextTick(() => { this.$refs.tree.setCurrentKey(this.currentId) }) } })
最终效果: