vue+elementui搭建后台管理界面(5递归生成侧栏路由) vue定义定义多级路由菜单

本文主要是介绍vue+elementui搭建后台管理界面(5递归生成侧栏路由) vue定义定义多级路由菜单,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

有一个菜单树,顶层菜单下面有多个子菜单,子菜单下还有子菜单。。。
这时候就要用递归处理

1 定义多级菜单

修改 src/router/index.js 的 / 路由

  {path: '/',redirect: '/dashboard',name: 'Container',component: Container,children: [{path: 'dashboard', name: '首页', component: Dashboard, children: [{path: 'dashboard1', name: '首页1', component: Dashboard,},{path: 'dashboard2', name: '首页2', component: Dashboard,children: [{path: 'dashboard21', name: '首页21', component: Dashboard,},{path: 'dashboard22', name: '首页22', component: Dashboard, },] },]},{path: 'article', name: '文章', component: Article, },]}

2 抽出Sidebar组件

生成的递归路由放在侧边栏,因此抽取 sidebar 组件,sidebar 包含logo和 递归路由
再抽取 SidebarItem 为单独的路由组件,方便递归调用

2.1 Sidebar

Sidebar 接收collapse、路由数组,同时引入 SidebarItem 组件

子组件传入:

  • barIdx: 当前路由的索引,用来定位子菜单
  • subroute: 路由对象
  • fatherpath: 父路径,如 /、 /a/b
<template><div><div class="app-side-logo"><img src="@/assets/logo.png":width="collapse ? '60' : '60'"height="60" /></div><el-menu class="el-menu-vertical-demo":default-active="defaultActive"router:collapse="collapse"><SidebarItem v-for="(item, idx) in routes" :subroute="item":fatherpath="fatherPath":barIdx="idx" :key="idx" /></el-menu></div>
</template><script>
import SidebarItem from './SidebarItem'
export default {naem: "Sidebar",components: {SidebarItem},props: {collapse: {type: Boolean},routes: {type: Array}},computed: {// 首次进入页面时展开当前页面所属的菜单defaultActive(){return this.$route.path},fatherPath(){// 这里直接获取路由配置的 '/' 项return this.$router.options.routes[1].path}}
}
</script><style></style>

2.2 SidebarItem

SidebarItem 接收路由、父路由path、父级idx,然后递归调用自身

<template><!-- 如果当前 subroute 有子节点 --><el-submenu v-if="!subroute.hidden && subroute.children && subroute.children.length > 0":index="genPath(fatherpath, subroute.path)"><!-- 创建菜单分组 --><template slot="title"><i class="el-icon-menu"></i><span slot="title">{{subroute.name}}</span></template><!-- 递归调用自身,直到 subroute 不含子节点 --><SidebarItem v-for="(submenu, subidx) in subroute.children":subroute="submenu":fatherpath="genPath(fatherpath, subroute.path)":barIdx="subidx" :key="barIdx + '-' + subidx" /></el-submenu><!-- 当前节点不含子节点且非隐藏 --><el-menu-item style="font-weight: 400"v-else-if="!subroute.hidden":index="genPath(fatherpath, subroute.path)">{{subroute.name}}</el-menu-item><el-menu-item style="font-weight: 400"v-else:index="genPath(fatherpath, subroute.path)">{{ subroute.name }}</el-menu-item>
</template><script>
export default {name: 'SidebarItem',props: {subroute: {type: Object},barIdx: {type: [String, Number]},fatherpath: {type: String}},computed: {// 默认激活的路由, 用来激活菜单选中状态defaultActive: function(){return this.$route.path},},methods: {// 生成侧边栏路由,格式: /a/b/cgenPath: function(){let arr = [ ...arguments ]let path = arr.map(v => {while (v[0] === '/'){v = v.substring(1)}while(v[-1] === '/'){v = v.substring(0, v.length)}return v }).join('/')path = path[0] === '/' ? path : '/'+pathreturn path},handleOpen: function(key, keyPath) {console.log(key, keyPath)},handleClose: function(key, keyPath) {console.log(key, keyPath)}},mounted: function(){console.log('sidebar routes: ', this.routes)}
}
</script><style>
</style>

3 项目结构

此时 src 的目录结构

│  App.vue
│  main.js
├─assets
│      logo.png
├─components
│      HelloWorld.vue
│      Sidebar.vue
│      SidebarItem.vue
├─container
│      Container.vue
├─router
│      index.js
├─styles
│      index.scss
└─views│  TheLogin.vue├─article│      index.vue└─dashboardindex.vue

4 修改容器配置

src/container/Container.vue 引入 Sidebar 组件

<template>
<!-- ... -->
<el-aside class="app-side app-side-left":class="isCollapse ? 'app-side-collapsed' : 'app-side-expanded'"><Sidebar :collapse="isCollapse" :routes="$router.options.routes[1].children"/>
</el-aside>
<!-- ... -->
</template>
<script>
import Sidebar from '@/components/Sidebar'
export default {name: 'Container',components: {Sidebar},
/** ... */
</script>

5 页面效果

页面效果如下

这篇关于vue+elementui搭建后台管理界面(5递归生成侧栏路由) vue定义定义多级路由菜单的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1088910

相关文章

Vue和React受控组件的区别小结

《Vue和React受控组件的区别小结》本文主要介绍了Vue和React受控组件的区别小结,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学... 目录背景React 的实现vue3 的实现写法一:直接修改事件参数写法二:通过ref引用 DOMVu

Java使用Javassist动态生成HelloWorld类

《Java使用Javassist动态生成HelloWorld类》Javassist是一个非常强大的字节码操作和定义库,它允许开发者在运行时创建新的类或者修改现有的类,本文将简单介绍如何使用Javass... 目录1. Javassist简介2. 环境准备3. 动态生成HelloWorld类3.1 创建CtC

Java实现将HTML文件与字符串转换为图片

《Java实现将HTML文件与字符串转换为图片》在Java开发中,我们经常会遇到将HTML内容转换为图片的需求,本文小编就来和大家详细讲讲如何使用FreeSpire.DocforJava库来实现这一功... 目录前言核心实现:html 转图片完整代码场景 1:转换本地 HTML 文件为图片场景 2:转换 H

C#使用Spire.Doc for .NET实现HTML转Word的高效方案

《C#使用Spire.Docfor.NET实现HTML转Word的高效方案》在Web开发中,HTML内容的生成与处理是高频需求,然而,当用户需要将HTML页面或动态生成的HTML字符串转换为Wor... 目录引言一、html转Word的典型场景与挑战二、用 Spire.Doc 实现 HTML 转 Word1

Vue3绑定props默认值问题

《Vue3绑定props默认值问题》使用Vue3的defineProps配合TypeScript的interface定义props类型,并通过withDefaults设置默认值,使组件能安全访问传入的... 目录前言步骤步骤1:使用 defineProps 定义 Props步骤2:设置默认值总结前言使用T

SpringBoot 多环境开发实战(从配置、管理与控制)

《SpringBoot多环境开发实战(从配置、管理与控制)》本文详解SpringBoot多环境配置,涵盖单文件YAML、多文件模式、MavenProfile分组及激活策略,通过优先级控制灵活切换环境... 目录一、多环境开发基础(单文件 YAML 版)(一)配置原理与优势(二)实操示例二、多环境开发多文件版

使用docker搭建嵌入式Linux开发环境

《使用docker搭建嵌入式Linux开发环境》本文主要介绍了使用docker搭建嵌入式Linux开发环境,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面... 目录1、前言2、安装docker3、编写容器管理脚本4、创建容器1、前言在日常开发全志、rk等不同

深度解析Python中递归下降解析器的原理与实现

《深度解析Python中递归下降解析器的原理与实现》在编译器设计、配置文件处理和数据转换领域,递归下降解析器是最常用且最直观的解析技术,本文将详细介绍递归下降解析器的原理与实现,感兴趣的小伙伴可以跟随... 目录引言:解析器的核心价值一、递归下降解析器基础1.1 核心概念解析1.2 基本架构二、简单算术表达

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

SpringBoot集成XXL-JOB实现任务管理全流程

《SpringBoot集成XXL-JOB实现任务管理全流程》XXL-JOB是一款轻量级分布式任务调度平台,功能丰富、界面简洁、易于扩展,本文介绍如何通过SpringBoot项目,使用RestTempl... 目录一、前言二、项目结构简述三、Maven 依赖四、Controller 代码详解五、Service