问题描述:
当el-tree 需要从后台拿到数据动态展示出来时,会出现一个父节点下面的所有子节点全部被勾选了,如图:

解决方案:
<el-tree
ref="tree"
:check-strictly="checkStrictly"
:data="routesData"
:props="defaultProps"
show-checkbox
node-key="path"
class="permission-tree"
/>
1.设置check-strictly 是一个变量checkStrictly 默认值是false
2.拿到接口数据后:
this.checkStrictly = true //重点:给数节点赋值之前 先设置为true
this.$nextTick(() => {
const routes = this.generateRoutes(this.role.routes) //拿到接口数据
this.$refs.tree.setCheckedNodes(routes) //给树节点赋值
this.checkStrictly = false //重点: 赋值完成后 设置为false
})
本文介绍如何在Element UI的El-Tree组件中,避免后台动态加载数据时出现父节点下所有子节点被自动勾选的问题。通过设置check-strictly属性并在合适时机切换其值,实现正确展示数据并控制节点选择状态。
8923





