|
1
2
3
|
<template>
<el-breadcrumb class="app-breadcrumb" separator="/">
<transition-group name="breadcrumb">
|
|
4
|
<el-breadcrumb-item v-for="(item, index) in levelList" :key="item.path">
|
胡菁
authored
|
5
6
7
8
9
|
<span
v-if="item.redirect === 'noRedirect' || index == levelList.length - 1"
class="no-redirect"
>{{ $t(item.meta.title) }}</span
>
|
|
10
11
12
|
<a v-else @click.prevent="handleLink(item)">{{
$t(item.meta.title)
}}</a>
|
|
13
14
15
16
17
18
|
</el-breadcrumb-item>
</transition-group>
</el-breadcrumb>
</template>
<script>
|
|
19
|
import pathToRegexp from "path-to-regexp";
|
赖素文
authored
|
20
|
import { getPermissionData } from "@/router/index";
|
|
21
22
23
|
export default {
data() {
return {
|
|
24
25
|
levelList: null,
};
|
|
26
27
28
|
},
watch: {
$route() {
|
|
29
30
|
this.getBreadcrumb();
},
|
|
31
32
|
},
created() {
|
|
33
|
this.getBreadcrumb();
|
|
34
35
36
37
|
},
methods: {
getBreadcrumb() {
// only show routes with meta.title
|
|
38
39
40
41
|
let matched = this.$route.matched.filter(
(item) => item.meta && item.meta.title
);
const first = matched[0];
|
|
42
|
|
|
43
|
if (!this.isMain(first)) {
|
|
44
45
46
|
matched = [
{ path: "/", meta: { title: this.$t("system.mainPage") } },
].concat(matched);
|
|
47
48
|
}
|
|
49
50
51
|
this.levelList = matched.filter(
(item) => item.meta && item.meta.title && item.meta.breadcrumb !== false
);
|
|
52
|
},
|
|
53
54
|
isMain(route) {
const name = route && route.name;
|
|
55
|
if (!name) {
|
|
56
|
return false;
|
|
57
|
}
|
|
58
|
return name.trim().toLocaleLowerCase() === "main".toLocaleLowerCase();
|
|
59
60
61
|
},
pathCompile(path) {
// To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
|
|
62
63
64
|
const { params } = this.$route;
var toPath = pathToRegexp.compile(path);
return toPath(params);
|
|
65
66
|
},
handleLink(item) {
|
赖素文
authored
|
67
68
69
70
71
72
73
74
|
getPermissionData(() => {
const { redirect, path } = item;
if (redirect) {
this.$router.push(redirect);
return;
}
this.$router.push(this.pathCompile(path));
});
|
|
75
76
77
|
},
},
};
|
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
</script>
<style lang="scss" scoped>
.app-breadcrumb.el-breadcrumb {
display: inline-block;
font-size: 14px;
line-height: 50px;
margin-left: 8px;
.no-redirect {
color: #97a8be;
cursor: text;
}
}
</style>
|