el-tree数据回显只选子节点父节点也会被选中解决办法

问题描述:

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

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
      })