jinjianback1.1/src/components/Breadcrumb/index.vue

79 lines
2.0 KiB
Vue
Raw Normal View History

2017-06-26 13:38:24 +08:00
<template>
2017-11-24 14:01:54 +08:00
<el-breadcrumb class="app-breadcrumb" separator="/">
2017-11-24 16:30:40 +08:00
<transition-group name="breadcrumb">
2019-01-17 11:40:17 +08:00
<el-breadcrumb-item v-for="(item,index) in levelList" :key="item.path">
2019-04-19 20:41:52 +08:00
<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>
2017-11-24 16:30:40 +08:00
</el-breadcrumb-item>
</transition-group>
2017-06-26 13:38:24 +08:00
</el-breadcrumb>
</template>
<script>
import pathToRegexp from 'path-to-regexp'
2017-08-30 17:42:06 +08:00
export default {
data() {
return {
levelList: null
}
},
2017-11-24 17:21:32 +08:00
watch: {
$route() {
this.getBreadcrumb()
}
},
2018-08-20 15:45:15 +08:00
created() {
this.getBreadcrumb()
},
2017-08-30 17:42:06 +08:00
methods: {
getBreadcrumb() {
2019-04-19 20:41:52 +08:00
// only show routes with meta.title
let matched = this.$route.matched.filter(item => item.meta && item.meta.title)
2017-08-30 17:42:06 +08:00
const first = matched[0]
2019-04-19 20:41:52 +08:00
if (!this.isDashboard(first)) {
2017-11-24 17:08:30 +08:00
matched = [{ path: '/dashboard', meta: { title: 'Dashboard' }}].concat(matched)
2017-06-26 13:38:24 +08:00
}
2019-01-17 11:40:17 +08:00
this.levelList = matched.filter(item => item.meta && item.meta.title && item.meta.breadcrumb !== false)
},
2019-04-19 20:41:52 +08:00
isDashboard(route) {
const name = route && route.name
if (!name) {
return false
}
return name.trim().toLocaleLowerCase() === 'Dashboard'.toLocaleLowerCase()
},
pathCompile(path) {
// To solve this problem https://github.com/PanJiaChen/vue-element-admin/issues/561
const { params } = this.$route
var toPath = pathToRegexp.compile(path)
return toPath(params)
},
handleLink(item) {
const { redirect, path } = item
if (redirect) {
this.$router.push(redirect)
return
}
this.$router.push(this.pathCompile(path))
2017-08-30 17:42:06 +08:00
}
}
}
2017-06-26 13:38:24 +08:00
</script>
2019-04-19 20:41:52 +08:00
<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;
2017-08-30 17:42:06 +08:00
}
2019-04-19 20:41:52 +08:00
}
2017-06-26 13:38:24 +08:00
</style>