elementui的tree默认高亮显示无效的问题

elementui的tree默认高亮显示无效的问题

必须要的属性:

1, 设置node-key 属性

2, 使用nextTick

3, 设置 highlight-current 属性

4, this.$refs.xxx.setCurrentKey('id名称')

复制代码
<template>
  <div class="onlineText">
    <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" default-expand-all 
    ref="treeList" node-key="id" highlight-current></el-tree>
  </div>
</template>

<script>
export default {
  name: "onlineText",
  data(){
    return{
      data: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, {
          id: 2,
          label: '一级 2',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        }, {
          id: 3,
          label: '一级 3',
          children: [{
            id: 7,
            label: '二级 3-1'
          }, {
            id: 8,
            label: '二级 3-2'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        }
    }
  },
  methods:{
    handleNodeClick(data) {
        console.log(data);
      }
  },
  mounted(){
    this.$nextTick(()=>{
      this.$refs.treeList.setCurrentKey('5')
    })
  }

};
</script>

<style lang="less">
.onlineText{
  
}
</style> 

这种就是设置默认值,也就是说不管你选择了哪个,刷新以后还是默认值。如果你想刷新以后保持你上一个点击的,就需要用缓存

复制代码
<template>
  <div class="onlineText">
    <el-tree :data="data" :props="defaultProps" @node-click="handleNodeClick" default-expand-all 
    ref="treeList" node-key="id" highlight-current></el-tree>
  </div>
</template>

<script>
export default {
  name: "onlineText",
  data(){
    return{
      data: [{
          id: 1,
          label: '一级 1',
          children: [{
            id: 4,
            label: '二级 1-1',
            children: [{
              id: 9,
              label: '三级 1-1-1'
            }, {
              id: 10,
              label: '三级 1-1-2'
            }]
          }]
        }, {
          id: 2,
          label: '一级 2',
          children: [{
            id: 5,
            label: '二级 2-1'
          }, {
            id: 6,
            label: '二级 2-2'
          }]
        }, {
          id: 3,
          label: '一级 3',
          children: [{
            id: 7,
            label: '二级 3-1'
          }, {
            id: 8,
            label: '二级 3-2'
          }]
        }],
        defaultProps: {
          children: 'children',
          label: 'label'
        },
        defaultId:'5'
    }
  },
  methods:{
    handleNodeClick(data) {
        console.log(data);
        sessionStorage.setItem("defaultId",data.id)

      }
  },
  mounted(){
    this.$nextTick(()=>{
      if(sessionStorage.getItem("defaultId")){
        this.$refs.treeList.setCurrentKey(sessionStorage.getItem("defaultId"))
      }else{
        this.$refs.treeList.setCurrentKey(this.defaultId)
      }
    })
  }

};
</script>

<style lang="less">
.onlineText{
  
}
</style> 
相关推荐
棉花糖超人21 分钟前
【从0-1的HTML】第2篇:HTML标签
前端·html
exploration-earth29 分钟前
本地优先的状态管理与工具选型策略
开发语言·前端·javascript
OpenTiny社区1 小时前
开源之夏报名倒计时3天!还有9个前端任务有余位,快来申请吧~
前端·github
ak啊1 小时前
WebGL魔法:从立方体到逼真阴影的奇妙之旅
前端·webgl
hang_bro1 小时前
使用js方法实现阻止按钮的默认点击事件&触发默认事件
前端·react.js·html
哈贝#1 小时前
vue和uniapp聊天页面右侧滚动条自动到底部
javascript·vue.js·uni-app
用户90738703648641 小时前
pnpm是如何解决幻影依赖的?
前端
树上有只程序猿1 小时前
Claude 4提升码农生产力的5种高级方式
前端
傻球1 小时前
没想到干前端2年了还能用上高中物理运动学知识
前端·react.js·开源
咚咚咚ddd1 小时前
前端组件:pc端通用新手引导组件最佳实践(React)
前端·react.js