long
2021-06-07 9c6d515baaaeaf34e773d44fd646459691fbb2bc
提交 | 用户 | age
2a61f6 1 <template>
L 2   <el-breadcrumb class="app-breadcrumb" separator="/">
3     <transition-group name="breadcrumb">
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>
6         <a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
7       </el-breadcrumb-item>
8     </transition-group>
9   </el-breadcrumb>
10 </template>
11
12 <script>
13 import pathToRegexp from 'path-to-regexp'
14
15 export default {
16   data() {
17     return {
18       levelList: null
19     }
20   },
21   watch: {
22     $route() {
23       this.getBreadcrumb()
24     }
25   },
26   created() {
27     this.getBreadcrumb()
28   },
29   methods: {
30     getBreadcrumb() {
31       // 只使用meta.title显示路由
32       let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
33       const first = matched[0]
34
35       if (!this.isDashboard(first)) {
36         matched = [{ path: '/dashboard', meta: { title: '首页' }}].concat(matched)
37       }
38
39       this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
40     },
41     isDashboard(route) {
42       const name = route && route.name
43       if (!name) {
44         return false
45       }
46       return name.trim().toLocaleLowerCase() === '首页'.toLocaleLowerCase()
47     },
48     pathCompile(path) {
49       // To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
50       const { params } = this.$route
51       var toPath = pathToRegexp.compile(path)
52       return toPath(params)
53     },
54     handleLink(item) {
55       const { redirect, path } = item
56       if (redirect) {
57         this.$router.push(redirect)
58         return
59       }
60       this.$router.push(this.pathCompile(path))
61     }
62   }
63 }
64 </script>
65
66 <style lang="scss" scoped>
67 .app-breadcrumb.el-breadcrumb {
68   display: inline-block;
69   font-size: 14px;
70   line-height: 50px;
71   margin-left: 8px;
72
73   .no-redirect {
74     color: #97a8be;
75     cursor: text;
76   }
77 }
78 </style>