Files
dm-manage-web/src/components/Breadcrumb/index.vue

80 lines
1.9 KiB
Vue
Raw Normal View History

2023-02-15 09:17:05 +08:00
<template>
<el-breadcrumb class="app-breadcrumb" separator="/">
<transition-group name="breadcrumb">
<el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
<span v-if="item.redirect === 'noRedirect' || index == levelList.length - 1" class="no-redirect">{{ item.meta.title }}</span>
<a v-else @click.prevent="handleLink(item)">{{ item.meta.title }}</a>
</el-breadcrumb-item>
</transition-group>
</el-breadcrumb>
</template>
<script>
export default {
data() {
return {
levelList: null
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
},
watch: {
$route(route) {
// if you go to the redirect page, do not update the breadcrumbs
if (route.path.startsWith('/redirect/')) {
2023-03-21 00:53:28 +08:00
return;
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
this.getBreadcrumb();
2023-02-15 09:17:05 +08:00
}
},
created() {
2023-03-21 00:53:28 +08:00
this.getBreadcrumb();
2023-02-15 09:17:05 +08:00
},
methods: {
getBreadcrumb() {
// only show routes with meta.title
2023-03-21 00:53:28 +08:00
let matched = this.$route.matched.filter((item) => item.meta && item.meta.title);
const first = matched[0];
2023-02-15 09:17:05 +08:00
if (!this.isDashboard(first)) {
2023-03-21 00:53:28 +08:00
matched = [
{
path: '/index',
meta: { title: '首页' }
}
].concat(matched);
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
this.levelList = matched.filter((item) => item.meta && item.meta.title && item.meta.breadcrumb !== false);
2023-02-15 09:17:05 +08:00
},
isDashboard(route) {
2023-03-21 00:53:28 +08:00
const name = route && route.name;
2023-02-15 09:17:05 +08:00
if (!name) {
2023-03-21 00:53:28 +08:00
return false;
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
return name.trim() === 'Index';
2023-02-15 09:17:05 +08:00
},
handleLink(item) {
2023-03-21 00:53:28 +08:00
const { redirect, path } = item;
2023-02-15 09:17:05 +08:00
if (redirect) {
2023-03-21 00:53:28 +08:00
this.$router.push(redirect);
return;
2023-02-15 09:17:05 +08:00
}
2023-03-21 00:53:28 +08:00
this.$router.push(path);
2023-02-15 09:17:05 +08:00
}
}
2023-03-21 00:53:28 +08:00
};
2023-02-15 09:17:05 +08:00
</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>