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

相关文章

C 语言中enum枚举的定义和使用小结

《C语言中enum枚举的定义和使用小结》在C语言里,enum(枚举)是一种用户自定义的数据类型,它能够让你创建一组具名的整数常量,下面我会从定义、使用、特性等方面详细介绍enum,感兴趣的朋友一起看... 目录1、引言2、基本定义3、定义枚举变量4、自定义枚举常量的值5、枚举与switch语句结合使用6、枚

HTML5中的Microdata与历史记录管理详解

《HTML5中的Microdata与历史记录管理详解》Microdata作为HTML5新增的一个特性,它允许开发者在HTML文档中添加更多的语义信息,以便于搜索引擎和浏览器更好地理解页面内容,本文将探... 目录html5中的Mijscrodata与历史记录管理背景简介html5中的Microdata使用M

html5的响应式布局的方法示例详解

《html5的响应式布局的方法示例详解》:本文主要介绍了HTML5中使用媒体查询和Flexbox进行响应式布局的方法,简要介绍了CSSGrid布局的基础知识和如何实现自动换行的网格布局,详细内容请阅读本文,希望能对你有所帮助... 一 使用媒体查询响应式布局        使用的参数@media这是常用的

HTML5表格语法格式详解

《HTML5表格语法格式详解》在HTML语法中,表格主要通过table、tr和td3个标签构成,本文通过实例代码讲解HTML5表格语法格式,感兴趣的朋友一起看看吧... 目录一、表格1.表格语法格式2.表格属性 3.例子二、不规则表格1.跨行2.跨列3.例子一、表格在html语法中,表格主要通过< tab

Spring 基于XML配置 bean管理 Bean-IOC的方法

《Spring基于XML配置bean管理Bean-IOC的方法》:本文主要介绍Spring基于XML配置bean管理Bean-IOC的方法,本文给大家介绍的非常详细,对大家的学习或工作具有一... 目录一. spring学习的核心内容二. 基于 XML 配置 bean1. 通过类型来获取 bean2. 通过

Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案

《Vue3组件中getCurrentInstance()获取App实例,但是返回null的解决方案》:本文主要介绍Vue3组件中getCurrentInstance()获取App实例,但是返回nu... 目录vue3组件中getCurrentInstajavascriptnce()获取App实例,但是返回n

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

JS+HTML实现在线图片水印添加工具

《JS+HTML实现在线图片水印添加工具》在社交媒体和内容创作日益频繁的今天,如何保护原创内容、展示品牌身份成了一个不得不面对的问题,本文将实现一个完全基于HTML+CSS构建的现代化图片水印在线工具... 目录概述功能亮点使用方法技术解析延伸思考运行效果项目源码下载总结概述在社交媒体和内容创作日益频繁的

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

前端CSS Grid 布局示例详解

《前端CSSGrid布局示例详解》CSSGrid是一种二维布局系统,可以同时控制行和列,相比Flex(一维布局),更适合用在整体页面布局或复杂模块结构中,:本文主要介绍前端CSSGri... 目录css Grid 布局详解(通俗易懂版)一、概述二、基础概念三、创建 Grid 容器四、定义网格行和列五、设置行