作者 RuoYi

菜单面包屑导航支持多层级显示

1 <template> 1 <template>
2 <el-breadcrumb class="app-breadcrumb" separator="/"> 2 <el-breadcrumb class="app-breadcrumb" separator="/">
3 <transition-group name="breadcrumb"> 3 <transition-group name="breadcrumb">
4 - <el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path"> 4 + <el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
5 <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span> 5 <span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
6 <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a> 6 <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
7 </el-breadcrumb-item> 7 </el-breadcrumb-item>
@@ -31,14 +31,41 @@ export default { @@ -31,14 +31,41 @@ export default {
31 methods: { 31 methods: {
32 getBreadcrumb() { 32 getBreadcrumb() {
33 // only show routes with meta.title 33 // only show routes with meta.title
34 - let matched = this.$route.matched.filter(item => item.meta && item.meta.title)  
35 - const first = matched[0]  
36 -  
37 - if (!this.isDashboard(first)) {  
38 - matched = [{ path: '/index', meta: { title: '首页' }}].concat(matched) 34 + let matched = []
  35 + const router = this.$route
  36 + const pathNum = this.findPathNum(router.path)
  37 + // multi-level menu
  38 + if (pathNum > 2) {
  39 + const reg = /\/\w+/gi
  40 + const pathList = router.path.match(reg).map((item, index) => {
  41 + if (index !== 0) item = item.slice(1)
  42 + return item
  43 + })
  44 + this.getMatched(pathList, this.$store.getters.sidebarRouters, matched)
  45 + } else {
  46 + matched = router.matched.filter((item) => item.meta && item.meta.title)
  47 + }
  48 + if (!this.isDashboard(matched[0])) {
  49 + matched = [{ path: "/index", meta: { title: "首页" } }].concat(matched)
  50 + }
  51 + this.levelList = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false)
  52 + },
  53 + findPathNum(str, char = "/") {
  54 + let index = str.indexOf(char)
  55 + let num = 0
  56 + while (index !== -1) {
  57 + num++
  58 + index = str.indexOf(char, index + 1)
  59 + }
  60 + return num
  61 + },
  62 + getMatched(pathList, routeList, matched) {
  63 + let data = routeList.find((item) => item.path == pathList[0])
  64 + matched.push(data)
  65 + if (data.children && pathList.length) {
  66 + pathList.shift()
  67 + this.getMatched(pathList, data.children, matched)
39 } 68 }
40 -  
41 - this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)  
42 }, 69 },
43 isDashboard(route) { 70 isDashboard(route) {
44 const name = route && route.name 71 const name = route && route.name
@@ -65,7 +92,6 @@ export default { @@ -65,7 +92,6 @@ export default {
65 font-size: 14px; 92 font-size: 14px;
66 line-height: 50px; 93 line-height: 50px;
67 margin-left: 8px; 94 margin-left: 8px;
68 -  
69 .no-redirect { 95 .no-redirect {
70 color: #97a8be; 96 color: #97a8be;
71 cursor: text; 97 cursor: text;