el-tree设置节点默认选中并高亮效果

 <el-tree
        ref="tree"
        default-expand-all
        highlight-current
        check-on-click-node
        node-key="id"
        :data="treeData"
        :props="defaultProps"
        @node-click="nodeClick"
      >
      </el-tree>

  treeData: [],
      defaultProps: {
        children: "children",
        label: "columnName",
      },

      treeData: [],


    async getTree() {
      let obj = {};
      await columnList()
        .then((res) => {
          if (res.data && res.data.length > 0) {
            this.treeData = handleTree(res.data, "id");
            obj = this.treeData[0];
            this.$nextTick(() => {
              // todo 在这里设置
              this.$refs.tree.setCurrentKey(this.treeData[0].children[1].id)
            })
          } else {
            this.treeData = [];
            obj = {};
          }
          console.log("this.treeData", this.treeData)
        })
        .finally(() => {
          console.log(obj);
          this.nodeClick(obj);
        });
    },


  created() {
    this.getTree();
  },
  // 设置首行高亮效果
  this.$refs.tree.setCurrentKey(this.treeData[0].id)

猜你喜欢

转载自blog.csdn.net/m0_74149462/article/details/134294287