element-ui默認選中並高亮某一個樹節點

<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>element-ui</title>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
    <div id="app">
        <el-tree ref="tree" :data="data" :props="defaultProps" default-expand-all :expand-on-click-node="false"
            node-key="id" highlight-current @node-click="handleNodeClick"></el-tree>
    </div>
    <script>
        new Vue({
            el: "#app",
            data: {
                data: [{
                    id: '1',
                    label: '一級 1',
                    children: [{
                        id: '1-1',
                        label: '二級 1-1',
                        children: [{
                            id: '1-1-1',
                            label: '三級 1-1-1'
                        }]
                    }]
                }, {
                    id: '2',
                    label: '一級 2',
                    children: [{
                        id: '2-1',
                        label: '二級 2-1',
                        children: [{
                            id: '2-1-1',
                            label: '三級 2-1-1'
                        }]
                    }, {
                        id: '2-2',
                        label: '二級 2-2',
                        children: [{
                            id: '2-2-1',
                            label: '三級 2-2-1'
                        }]
                    }]
                }, {
                    id: '3',
                    label: '一級 3',
                    children: [{
                        id: '3-1',
                        label: '二級 3-1',
                        children: [{
                            id: '3-1-1',
                            label: '三級 3-1-1'
                        }]
                    }, {
                        id: '3-2',
                        label: '二級 3-2',
                        children: [{
                            id: '3-2-1',
                            label: '三級 3-2-1'
                        }]
                    }]
                }],
                defaultProps: {
                    children: 'children',
                    label: 'label'
                }
            },
            methods: {
                handleNodeClick(data) {
                    console.log(data);
                }
            },
            mounted() {
                this.$nextTick(function () {
                    this.$refs['tree'].setCurrentKey('1');
                })
            }
        })
    </script>
</body>

</html>

 

發佈了58 篇原創文章 · 獲贊 4 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章