有一个菜单树,顶层菜单下面有多个子菜单,子菜单下还有子菜单。。。

这时候就要用递归处理

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/c
genPath: 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 : '/'+path
return 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
└─dashboard
index.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>

页面效果



vue+elementui搭建后台管理界面(5递归生成侧栏路由)的更多相关文章

  1. vue+elementui搭建后台管理界面(7 vuex和mockjs的使用)

    将权限管理应用到系统,首先做好登录, 点击登录按钮后,触发以下动作 vuex 中的 login 动作,设置 cookie vuex 中的 getuserinfo , 获取权限.用户名.头像等 由于目前 ...

  2. vue+elementui搭建后台管理界面(2首页)

    1 会话存储 使用html5的 sessionStorage 对象临时保存会话 // 保存会话 sessionStorage.setItem('user', username) // 删除会话 ses ...

  3. vue+elementui搭建后台管理界面

    1 会话存储 使用html5的 sessionStorage 对象临时保存会话 // 保存会话 sessionStorage.setItem('user', username) // 删除会话 ses ...

  4. vue+elementui搭建后台管理界面(1登录)

    1 node环境安装 从 node官网下载安装包 2 vue-cli npm install vue-cli -g 3 新建项目 vue init webpack vue-project 可保持默认, ...

  5. vue+elementui搭建后台管理界面(8 同步/异步获取数据渲染table)

    elementui已经封装好了 el-table 组件,只需要指定 data 数据源即可,因此通常在 vue 实例生命周期的 created 阶段,从数据库获取数据,再将返回的数据绑定到 data 如 ...

  6. vue+elementui搭建后台管理界面(6登录和菜单权限控制)

    不同的权限对应不同的路由(菜单),同时侧边栏也根据权限异步生成,实现登录和鉴权思路如下: 登录:点击登录,服务器验证通过后返回一个 token ,然后存到 cookie,再根据 token 拉取用户权 ...

  7. vue+elementui搭建后台管理界面(6登录和菜单权限控制[二])

    根据权限计算路由的代码 /** * 通过meta.role判断是否与当前用户权限匹配 * @param roles * @param route */ function hasRoles (roles ...

  8. vue+elementui搭建后台管理界面(3侧边栏菜单)

    上一节搭好了主框架,但是标签页和侧边栏只是分别展示了各自的菜单,如何将二者联动起来? 定义路由规则:当有 children 属性时,从 children 里取出 path 填充到侧边栏,如: { pa ...

  9. vue+elementui搭建后台管理界面(4使用font-awesome)

    使用font-awesome npm install --save font-awesome 修改 src/main.js 增加 import 'font-awesome/scss/font-awes ...

随机推荐

  1. Kconfig和Makefile

    内核源码树的目录下都有Kconfig和Makefile.在内核配置make menuconfig时,从Kconfig中读出菜单,用户勾选后保存到.config中.在内核编译时,Makefile调用这个 ...

  2. Nginx记录post body内容

    nginx在记录http的body内容时,会将中文转义为16进制 在nginx 1.11.8 以上版本中log_format 增加了escape=json 参数,可以不转义变量内容: log_form ...

  3. 浅析MySQL使用 GROUP BY 分组聚合与细分聚合

    原创文章,转载请注明出处:http://www.cnblogs.com/weix-l/p/7521278.html: 若有错误,请评论指出,谢谢! 1. 聚合函数(Aggregate Function ...

  4. 2013.5.3 - KDD第十五天

    今天上午把昨天的想法给中秋发过去了,然后我就开始科普随机森林: 随机森林是一种比较新的机器学习模型.经典的机器学习模型是神经网络,有半个多世纪的历史了.神经网络预测精确,但是计算量很大.上世纪八十年代 ...

  5. #2590. 「NOIP2009」最优贸易

    C 国有 n 个大城市和 m 条道路,每条道路连接这 n 个城市中的某两个城市.任意两个城市之间最多只有一条道路直接相连.这 m 条道路中有一部分为单向通行的道路,一部分为双向通行的道路,双向通行的道 ...

  6. Java volatile关键字实现原理

    场景引入 可见性问题 先来看一张图: 上面的图,是简化版的Java内存模型,一个线程有自己的工作内存,同时还有一个共享的主内存. 线程1和线程2读取数据data时,先从主内存里加载data变量的值到工 ...

  7. spring依赖注入实例

    依赖注入: BL public class T01BL implements Serializable { private static final Log logger = LogFactory.g ...

  8. Windows窗体控件实现内容拖放(DragDrop)功能

    一.将控件内容拖到其他控件 在开发过程中,经常会有这样的要求,拖动一个控件的数据到另外一个控件中.例如将其中一个ListBox中的数据拖到另一个ListBox中.或者将DataGridView中的数据 ...

  9. Net线程间通信的异步机制

    线程间通信 我们看下面的图 我们来看线程间通信的原理:线程(Thread B)和线程(Thread A)通信, 首先线程A 必须实现同步上下文对象(Synchronization Context), ...

  10. iptables常用命令二之如何删除nat规则

    删除iptables nat 规则 删除FORWARD 规则: iptables -nL FORWARD --line-number iptables -D FORWARD 1 删除一条nat 规则 ...