el-menu如何根据多层树形结构递归遍历展示菜单栏

 更新时间:2024年07月24日 11:01:06   作者:m0_62317155  
这篇文章主要介绍了el-menu根据多层树形结构递归遍历展示菜单栏,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧

前提条件

package.json如下所示,这是一个Vite + Vue3 + TS的项目

{
  "name": "vue3-ts-vite-wen-zhang-ji-lu-xiang-mu",
  "private": true,
  "version": "0.0.0",
  "type": "module",
  "scripts": {
    "dev": "vite",
    "build": "vue-tsc && vite build",
    "preview": "vite preview"
  },
  "dependencies": {
    "element-plus": "^2.4.2",
    "vue": "^3.3.4"
  },
  "devDependencies": {
    "@vitejs/plugin-vue": "^4.2.3",
    "sass": "^1.69.5",
    "typescript": "^5.0.2",
    "vite": "^4.4.5",
    "vue-tsc": "^1.8.5"
  }
}

下面为了方便,直接在App.vue组件中,代码结构如下所示,就一纯净项目,然后直接在App.vue中写代码

假设菜单等级只有两个等级

如果菜单等级只有两个等级,那就没有必要使用到递归了,直接遍历,然后根据是否有children字段,判断是一级菜单还是二级菜单就可以了。具体代码如下所示:

<template>
  <div style="width: 100%; height: 100%;">
    <div class="common-layout">
      <el-container>
        <el-header>头部</el-header>
        <el-container>
          <!-- 侧边栏区域 -->
          <el-aside width="200px">
            <el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose">
              <template v-for="(item, index) in menuList" :key="index">
                <el-sub-menu :index="item.path" v-if="item.children && item.children.length">
                  <template #title>
                    <el-icon>
                      <location />
                    </el-icon>
                    <span>{{ item.name }}</span>
                  </template>
                  <el-menu-item v-for="child in item.children" :key="child.id" :index="child.path">
                    {{ child.name }}
                  </el-menu-item>
                </el-sub-menu>
                <el-menu-item v-else :index="item.path">
                  <el-icon><setting /></el-icon>
                  <span>{{ item.name }}</span>
                </el-menu-item>
              </template>
            </el-menu>
          </el-aside>
          <!-- 主题区域 -->
          <el-main>
            这是主题区域
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import { Location, Setting } from '@element-plus/icons-vue'
interface MenuItem {
  id: number;
  name: string;
  path: string;
  icon?: string;
  component?: string;
  children?: MenuItem[];
}
const menuList = ref<MenuItem[]>(
  [
    {
      id: 1,
      name: '首页',
      path: '/',
      icon: 'location',
      component: 'home',
      children: []
    },
    {
      id: 2,
      name: '用户管理',
      path: '/user',
      icon: 'location',
      component: 'user',
      children: [
        {
          id: 3,
          name: '用户列表',
          path: 'list',
          icon: '',
          component: 'userList',
          children: []
        },
        {
          id: 5,
          name: '角色列表',
          path: 'roleList',
          icon: '',
          component: 'userList',
          children: []
        }
      ]
    },
    {
      id: 6,
      name: '权限管理',
      path: '/permission',
      icon: 'setting',
      component: 'permission',
      children: [
        {
          id: 7,
          name: '权限列表',
          path: 'permissionList',
          icon: '',
          component: 'permissionList',
        }
      ]
    }
  ]
)
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
</script>
<style scoped lang="scss">
.el-container {
  width: 100%;
  height: 100%;
}
</style>

结果如下所示

在这里插入图片描述

但是如果菜单等级超过两个等级或者多个等级的话

但是如果菜单等级超过两个等级或者多个等级的话,这时就可以使用到组件递归的方式进行了。目录结构如下所示:

在这里插入图片描述

App.vue

<template>
  <div style="width: 100%; height: 100%;">
    <div class="common-layout">
      <el-container>
        <el-header>头部</el-header>
        <el-container>
          <!-- 侧边栏区域 -->
          <el-aside width="200px">
            <el-menu default-active="2" class="el-menu-vertical-demo" @open="handleOpen" @close="handleClose">
              <menu-items :items="menuList"></menu-items>
            </el-menu>
          </el-aside>
          <!-- 主题区域 -->
          <el-main>
            这是主题区域
          </el-main>
        </el-container>
      </el-container>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ref } from 'vue'
import MenuItems from './components/MenuItems.vue'
interface MenuItem {
  id: number;
  name: string;
  path: string;
  icon?: string;
  component?: string;
  children?: MenuItem[];
}
const menuList = ref<MenuItem[]>(
  [
    {
      id: 1,
      name: '首页',
      path: '/',
      icon: 'location',
      component: 'home',
      children: []
    },
    {
      id: 2,
      name: '用户管理',
      path: '/user',
      icon: 'location',
      component: 'user',
      children: [
        {
          id: 3,
          name: '用户列表',
          path: 'list',
          icon: '',
          component: 'userList',
          children: [
            {
              id: 4,
              name: '用户详情',
              path: 'userDetail',
              icon: '',
              component: 'userDetail',
              children: []
            }
          ]
        },
        {
          id: 5,
          name: '角色列表',
          path: 'roleList',
          icon: '',
          component: 'userList',
          children: []
        }
      ]
    },
    {
      id: 6,
      name: '权限管理',
      path: '/permission',
      icon: 'setting',
      component: 'permission',
      children: [
        {
          id: 7,
          name: '权限列表',
          path: 'permissionList',
          icon: '',
          component: 'permissionList',
          children: [
            {
              id: 8,
              name: '权限详情-1',
              path: 'permissionDetail',
              icon: '',
              component: 'permissionDetail',
              children: [
                {
                  id: 9,
                  name: '权限详情-2',
                  path: 'permissionDetail2',
                  icon: '',
                  component: 'permissionDetail2',
                  children: []
                }
              ]
            }
          ]
        }
      ]
    }
  ]
)
const handleOpen = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
const handleClose = (key: string, keyPath: string[]) => {
  console.log(key, keyPath)
}
</script>
<style scoped lang="scss">
.el-container {
  width: 100%;
  height: 100%;
}
</style>

MenuItems.vue

<template>
    <template v-for="item in items" :key="item.id">
        <el-sub-menu v-if="item.children && item.children.length > 0" :index="item.path">
            <template #title>
                <span>{{ item.name }}</span>
            </template>
            <!-- 递归遍历 -->
            <menu-items :items="item.children" />
        </el-sub-menu>
        <el-menu-item v-else :index="item.path">
            <span>{{ item.name }}</span>
        </el-menu-item>
    </template>
</template>
<script setup lang="ts">
interface MenuItem {
  id: number;
  name: string;
  path: string;
  icon?: string;
  component?: string;
  children?: MenuItem[];
}
defineProps<{
    items: MenuItem[];
}>()
</script>

结果如下所示

在这里插入图片描述

从图中可以看出,无论是一层,二层,三层,四层结构的树形数据,都可以在el-menu中展示。

关于遍历时图标前的展示后续完善

关于点击路由跳转参考element plus的官网即可

到此这篇关于el-menu根据多层树形结构递归遍历展示菜单栏的文章就介绍到这了,更多相关el-menu多层树形结构递归遍历展示菜单栏内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • 浅谈vue中使用图片懒加载vue-lazyload插件详细指南

    浅谈vue中使用图片懒加载vue-lazyload插件详细指南

    本篇文章主要介绍了浅谈vue中使用图片懒加载vue-lazyload插件详细指南,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。
    2017-10-10
  • Vue组件通信之父传子与子传父详细讲解

    Vue组件通信之父传子与子传父详细讲解

    这篇文章主要介绍了React中父子组件通信详解,在父组件中,为子组件添加属性数据,即可实现父组件向子组件通信,文章通过围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-10-10
  • 基于Vue.js实现一个完整的登录功能

    基于Vue.js实现一个完整的登录功能

    在现代Web应用中,用户登录功能是一个核心模块,它不仅涉及到用户身份验证,还需要处理表单验证、状态管理、接口调用等多个环节,本文将基于一个Vue.js项目中的登录功能实现,深入解析其背后的技术细节,帮助开发者更好地理解和实现类似功能,需要的朋友可以参考下
    2025-02-02
  • Vue模板语法v-bind教程示例

    Vue模板语法v-bind教程示例

    这篇文章主要为大家介绍了Vue模板语法v-bind教程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-12-12
  • 创建Vue项目以及引入Iview的方法示例

    创建Vue项目以及引入Iview的方法示例

    这篇文章主要介绍了创建Vue项目以及引入Iview的方法示例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-12-12
  • Vue.js使用带有对象的 v-model 来创建自定义组件的详细操作

    Vue.js使用带有对象的 v-model 来创建自定义组件的详细操作

    本文介绍了如何在Vue.js中使用带有对象的v-model来创建自定义组件,通过创建一个封装了多个输入字段的自定义组件,并使用计算属性和深拷贝来处理对象状态,可以实现双向数据绑定,感兴趣的朋友跟随小编一起看看吧
    2025-12-12
  • vue.js与后台数据交互的实例讲解

    vue.js与后台数据交互的实例讲解

    今天小编就为大家分享一篇vue.js与后台数据交互的实例讲解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-08-08
  • 一文详解Vue3中使用ref获取元素节点

    一文详解Vue3中使用ref获取元素节点

    这篇文章主要介绍了一文详解Vue3中使用ref获取元素节点,本文介绍在vue3的setup中使用composition API获取元素节点的几种方法,需要的朋友可以参考一下
    2022-07-07
  • Vue中灵活拖拽的前端神器VueDraggablePlus的用法详解

    Vue中灵活拖拽的前端神器VueDraggablePlus的用法详解

    这篇文章主要介绍了一款功能强大、灵活易用的前端组件VueDraggablePlus,作为前端工程师,我们经常会遇到需要实现拖拽功能的场景,而VueDraggablePlus正是为了解决这一痛点而诞生的,让我们一起来看看它的特点和用法吧
    2024-03-03
  • vue3 中使用 jsx 开发的详细过程

    vue3 中使用 jsx 开发的详细过程

    这篇文章主要介绍了vue3 中使用 jsx 开发,本文着重说一下在使用 .vue 文件和 .jsx 文件在使用语法上的差异,需要的朋友可以参考下
    2022-09-09

最新评论