Vue递归组件实现树形结构菜单

    Tree 组件是递归类组件的典型代表,它常用于文件夹、组织架构、生物分类、国家地区等等,世间万物的大多数结构都是树形结构。使用树控件可以完整展现其中的层级关系,并具有展开收起选择等交互功能。
    如图所示,我们要实现的就是这样一个效果。之前我们写树状结构都是用jQuery来实现的,用Vue怎么实现呢?

一、数据部分模拟
  menuList:[
            {
                title:'菜单1',
                children:[
                    {
                        title:'菜单1-1',
                        children:[
                                {title:'菜单1-1-1'},
                                {title:'菜单1-1-2'},
                                {title:'菜单1-1-3'}
                            ]
                        },
                    {title:'菜单1-2'},
                    {title:'菜单1-3'}
                ]
            },
            {title:'菜单2'},
            {title:'菜单3'}
    ]
  
复制代码
二、组件各部分实现

Menu.vue

首先我们来写个menu组件,这里放个ul列表,里面的内容,用插槽来表示。
<template>
    <ul>
       <slot></slot>
    </ul>
</template>

<script>
    export defau
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,可以通过递归重新生成树形结构的id。具体实现方法如下: 1. 首先,需要定义一个组件,用于展示树形结构。 2. 在组件中,定义一个方法,用于递归生成树形结构的id。该方法需要传入两个参数:当前节点的id和当前节点的子节点数组。 3. 在方法中,首先判断子节点数组是否为空,如果为空,则直接返回空数组。 4. 如果子节点数组不为空,则遍历子节点数组,对每个子节点进行递归调用,生成其对应的id,并将其存入一个新的数组中。 5. 最后,将当前节点的id和新生成的子节点id数组合并成一个新的数组,并返回该数组。 具体代码实现可以参考以下示例: ``` <template> <div> <ul> <li v-for="node in treeData" :key="node.id"> {{ node.name }} <tree-node :node="node" :pid="node.id" :pidStr="'parentId'" :list="treeData" /> </li> </ul> </div> </template> <script> import TreeNode from './TreeNode.vue'; export default { components: { TreeNode, }, data() { return { treeData: [ { id: 1, name: 'Node 1', parentId: 0 }, { id: 2, name: 'Node 2', parentId: 1 }, { id: 3, name: 'Node 3', parentId: 1 }, { id: 4, name: 'Node 4', parentId: 2 }, { id: 5, name: 'Node 5', parentId: 2 }, { id: 6, name: 'Node 6', parentId: 3 }, { id: 7, name: 'Node 7', parentId: 3 }, ], }; }, }; </script> ``` ``` <template> <ul v-if="children.length"> <li v-for="child in children" :key="child.id"> {{ child.name }} <tree-node :node="child" :pid="child.id" :pidStr="pidStr" :list="list" /> </li> </ul> </template> <script> import TreeNode from './TreeNode.vue'; export default { components: { TreeNode, }, props: { node: { type: Object, required: true, }, pid: { type: Number, required: true, }, pidStr: { type: String, required: true, }, list: { type: Array, required: true, }, }, computed: { children() { const children = this.list.filter(item => item[this.pidStr] === this.pid); return children.map(child => ({ ...child, children: this.initTree({ pid: child.id, pidStr: this.pidStr, list: this.list }), })); }, }, methods: { initTree({ pid, pidStr, list }) { const children = list.filter(item => item[pidStr] === pid); if (!children.length) { return []; } return children.map(child => ({ ...child, children: this.initTree({ pid: child.id, pidStr, list }), })); }, }, }; </script> ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值