現在可以實現,點擊某個節點,該節點會紅色高亮,那怎么讓這個樹加載出來的時候 默認某個節點高亮呢?element ui里面帶勾選框的可以默認勾選上,這個沒有勾選框
其實很簡單。element ui的樹形控件支持setCurrentKey()方法,只需要將你要設置的節點的key值設置進去就可以了,前提是要設置好node-key屬性。然后在created()或mounted()鈎子里寫上代碼就可以了。
<template>
<section class="p-10">
<el-tree :data="data" ref="vuetree" :props="defaultProps" @node-click="handleNodeClick" highlight-current node-key="id" default-expand-all />
</section>
</template>
<script> export default { 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(function(){ this.$refs['vuetree'].setCurrentKey(10); }) } }; </script>
嗯,就醬~~