breadcrumb.vue
1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<template>
<Breadcrumb v-if="showBreadcrumb">
<Breadcrumb-item v-bind:href="item.path" v-for="item in items" :key="item.name">{{item.name}}</Breadcrumb-item>
</Breadcrumb>
</template>
<script>
import _ from 'lodash';
export default {
name: 'breadcrumb',
data() {
return {
items: [],
activeName: '',
showBreadcrumb: false
};
},
created() {
_.remove(this.$router.beforeHooks, hook => hook.name === 'breadcrumb');
let that = this;
this.$router.beforeEach(function breadcrumb(to, from, next) {
if (to.name.indexOf('error.') < 0) {
that.showBreadcrumb = false;
that.$nextTick(() => { // Breadcrumb更新子组件的separator,如果不手动重建Breadcrumb周期,子组件separator不会得到更新
that.render(to);
});
}
return next();
});
this.render();
},
methods: {
render(to) {
this.showBreadcrumb = true;
this.items = [{name: '首页', path: '/'}];
let purviews = this.$config.purviews;
let routeName = to && to.name || this.$router.history.current.name;
let routes = _.split(routeName, '.');
_.each(routes, route => {
let findRoute = _.get(purviews, route);
if (findRoute) {
purviews = findRoute.subPurviews;
if (findRoute.name) {
this.items.push({
name: findRoute.name,
path: findRoute.path
});
}
}
});
_.last(this.items).path = '';
}
}
};
</script>
<style lang="scss">
</style>