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

相关文章

spring-boot-starter-thymeleaf加载外部html文件方式

《spring-boot-starter-thymeleaf加载外部html文件方式》本文介绍了在SpringMVC中使用Thymeleaf模板引擎加载外部HTML文件的方法,以及在SpringBoo... 目录1.Thymeleaf介绍2.springboot使用thymeleaf2.1.引入spring

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

前端原生js实现拖拽排课效果实例

《前端原生js实现拖拽排课效果实例》:本文主要介绍如何实现一个简单的课程表拖拽功能,通过HTML、CSS和JavaScript的配合,我们实现了课程项的拖拽、放置和显示功能,文中通过实例代码介绍的... 目录1. 效果展示2. 效果分析2.1 关键点2.2 实现方法3. 代码实现3.1 html部分3.2

SpringBoot中使用 ThreadLocal 进行多线程上下文管理及注意事项小结

《SpringBoot中使用ThreadLocal进行多线程上下文管理及注意事项小结》本文详细介绍了ThreadLocal的原理、使用场景和示例代码,并在SpringBoot中使用ThreadLo... 目录前言技术积累1.什么是 ThreadLocal2. ThreadLocal 的原理2.1 线程隔离2

Go路由注册方法详解

《Go路由注册方法详解》Go语言中,http.NewServeMux()和http.HandleFunc()是两种不同的路由注册方式,前者创建独立的ServeMux实例,适合模块化和分层路由,灵活性高... 目录Go路由注册方法1. 路由注册的方式2. 路由器的独立性3. 灵活性4. 启动服务器的方式5.

浅析如何使用Swagger生成带权限控制的API文档

《浅析如何使用Swagger生成带权限控制的API文档》当涉及到权限控制时,如何生成既安全又详细的API文档就成了一个关键问题,所以这篇文章小编就来和大家好好聊聊如何用Swagger来生成带有... 目录准备工作配置 Swagger权限控制给 API 加上权限注解查看文档注意事项在咱们的开发工作里,API

CSS弹性布局常用设置方式

《CSS弹性布局常用设置方式》文章总结了CSS布局与样式的常用属性和技巧,包括视口单位、弹性盒子布局、浮动元素、背景和边框样式、文本和阴影效果、溢出隐藏、定位以及背景渐变等,通过这些技巧,可以实现复杂... 一、单位元素vm 1vm 为视口的1%vh 视口高的1%vmin 参照长边vmax 参照长边re

CSS3中使用flex和grid实现等高元素布局的示例代码

《CSS3中使用flex和grid实现等高元素布局的示例代码》:本文主要介绍了使用CSS3中的Flexbox和Grid布局实现等高元素布局的方法,通过简单的两列实现、每行放置3列以及全部代码的展示,展示了这两种布局方式的实现细节和效果,详细内容请阅读本文,希望能对你有所帮助... 过往的实现方法是使用浮动加

css渐变色背景|<gradient示例详解

《css渐变色背景|<gradient示例详解》CSS渐变是一种从一种颜色平滑过渡到另一种颜色的效果,可以作为元素的背景,它包括线性渐变、径向渐变和锥形渐变,本文介绍css渐变色背景|<gradien... 使用渐变色作为背景可以直接将渐China编程变色用作元素的背景,可以看做是一种特殊的背景图片。(是作为背

本地搭建DeepSeek-R1、WebUI的完整过程及访问

《本地搭建DeepSeek-R1、WebUI的完整过程及访问》:本文主要介绍本地搭建DeepSeek-R1、WebUI的完整过程及访问的相关资料,DeepSeek-R1是一个开源的人工智能平台,主... 目录背景       搭建准备基础概念搭建过程访问对话测试总结背景       最近几年,人工智能技术