西麦农场

时间尺度守恒


  • 首页

  • 分类

  • 归档

  • 坑List

vue-element-admin的面包屑导航优化方案

2019-08-30 | DevFAQ | Vue Element vue-router

说明

在使用vue-element-admin时对于其中的面包屑导航有丢丢不满意。
如图:
vue element admin breadcrumb
我更希望的如下:
Dashboard>List
Dashboard>List>Detail

解决思路一

通过在router中添加children来实现。这样面包屑导航完美复合我的要求。但是因为Vue Router嵌套规则,此条路肯定走不通,同时vue element admin在这样显示菜单时也有问题。

解决思路二

在router的meta中添加parent属性(eg:parent:{name:’route.name’})。修改vue element admin的面包屑组件代码getBreadcrumb方法。
原始

  1. getBreadcrumb() {
  2. // only show routes with meta.title
  3. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  4. const first = matched[0]
  5. if (!this.isDashboard(first)) {
  6. matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
  7. }
  8. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  9. }

修改为:

  1. getBreadcrumb() {
  2. // only show routes with meta.title
  3. let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
  4. const first = matched[0]
  5. if (!this.isDashboard(first)) {
  6. matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
  7. }
  8. if (matched.length > 1) {
  9. const insertPosition = matched.length - 1
  10. let parent = matched[insertPosition].meta.parent
  11. while (parent) {
  12. const route = this.$router.resolve(parent).route
  13. matched.splice(insertPosition, 0, route)
  14. parent = route.meta.parent
  15. }
  16. }
  17. this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  18. }

至此,基本满足我的需求

  • 目录
  • 站点概览
  • 说明
  • 解决思路一
  • 解决思路二
    戊在西安填坑中

    戊在西安填坑中

    29 日志
    7 分类
    50 标签
    0%
    © 2017- 戊在西安填坑中 | 陕ICP备13008306号-3 | 主题 NexT.Pisces