首先:在el-tree 标签上绑定官方属性current-node-key="",如下代码所示:

<el-tree accordion ref="treeRef" :data="myData.treeData" :props="treeProps" :current-node-key="curTreeId" node-key="id" @node-click="methods.handleNodeClick" highlight-current />

其次:获取该el-tree dom节点后赋值即可,如下代码所示:

<template>
    <div class="container" v-wechat-title="$route.meta.title">
        
    <div class="tree_box">
        <el-tree accordion ref="treeRef" :data="myData.treeData" :props="treeProps" :current-node-key="myData.curTreeId" node-key="id" @node-click="methods.handleNodeClick"
            highlight-current />
    </div>
</div>
</template>
 
<script setup>
 
import { ref,reactive, onMounted,nextTick } from 'vue';
//创建变量treeRef并赋值,同时html模板<el-tree ref="treeRef" />上的ref=treeRef,这样才可以获取到该dom节点。获取dom节点还是要treeRef.value才可以
let treeRef=ref(null)

const myData=reactive({
    curTreeId:''
})
//配置el-tree--props
const treeProps = {
    children: 'children',
    label: 'title',
    value:'id',
}
onMounted(() => {
    //默认展开el-tree指定的节点
    nextTick(() => {
       myData.curTreeId=3
       treeRef.value.setCurrentKey(myData.curTreeId)
    })
})
 
</script>

如有不清楚的,欢迎留言。。。

Logo

前往低代码交流专区

更多推荐