2022-04-21 18:20:39 +08:00
|
|
|
<script lang="ts" setup>
|
2022-04-22 01:16:02 +08:00
|
|
|
import { onBeforeMount, reactive, watch } from "vue"
|
|
|
|
import { useRoute, useRouter, RouteLocationMatched } from "vue-router"
|
|
|
|
import { compile } from "path-to-regexp"
|
2022-04-21 18:20:39 +08:00
|
|
|
|
2022-04-22 12:47:04 +08:00
|
|
|
const route = useRoute()
|
2022-04-21 18:20:39 +08:00
|
|
|
const router = useRouter()
|
2022-04-22 12:47:04 +08:00
|
|
|
|
2022-04-21 18:20:39 +08:00
|
|
|
const pathCompile = (path: string) => {
|
2022-04-22 12:47:04 +08:00
|
|
|
const { params } = route
|
2022-04-21 18:20:39 +08:00
|
|
|
const toPath = compile(path)
|
|
|
|
return toPath(params)
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = reactive({
|
|
|
|
breadcrumbs: [] as Array<RouteLocationMatched>,
|
|
|
|
getBreadcrumb: () => {
|
2022-04-22 12:47:04 +08:00
|
|
|
const matched = route.matched.filter((item) => item.meta && item.meta.title)
|
2022-04-21 18:20:39 +08:00
|
|
|
state.breadcrumbs = matched.filter((item) => {
|
|
|
|
return item.meta && item.meta.title && item.meta.breadcrumb !== false
|
|
|
|
})
|
|
|
|
},
|
|
|
|
handleLink(item: any) {
|
|
|
|
const { redirect, path } = item
|
|
|
|
if (redirect) {
|
|
|
|
router.push(redirect).catch((err) => {
|
|
|
|
console.warn(err)
|
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
router.push(pathCompile(path)).catch((err) => {
|
|
|
|
console.warn(err)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
watch(
|
2022-04-22 12:47:04 +08:00
|
|
|
() => route.path,
|
2022-04-21 18:20:39 +08:00
|
|
|
(path) => {
|
2022-04-22 01:16:02 +08:00
|
|
|
if (path.startsWith("/redirect/")) {
|
2022-04-21 18:20:39 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
state.getBreadcrumb()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
onBeforeMount(() => {
|
|
|
|
state.getBreadcrumb()
|
|
|
|
})
|
|
|
|
</script>
|
|
|
|
|
2022-04-22 12:47:04 +08:00
|
|
|
<template>
|
|
|
|
<el-breadcrumb class="app-breadcrumb">
|
|
|
|
<transition-group name="breadcrumb">
|
|
|
|
<el-breadcrumb-item v-for="(item, index) in state.breadcrumbs" :key="item.path">
|
|
|
|
<span v-if="item.redirect === 'noRedirect' || index === state.breadcrumbs.length - 1" class="no-redirect">{{
|
|
|
|
item.meta.title
|
|
|
|
}}</span>
|
|
|
|
<a v-else @click.prevent="state.handleLink(item)">
|
|
|
|
{{ item.meta.title }}
|
|
|
|
</a>
|
|
|
|
</el-breadcrumb-item>
|
|
|
|
</transition-group>
|
|
|
|
</el-breadcrumb>
|
|
|
|
</template>
|
|
|
|
|
2022-04-21 18:20:39 +08:00
|
|
|
<style lang="scss" scoped>
|
|
|
|
.el-breadcrumb__inner,
|
|
|
|
.el-breadcrumb__inner a {
|
|
|
|
font-weight: 400 !important;
|
|
|
|
}
|
|
|
|
|
|
|
|
.app-breadcrumb.el-breadcrumb {
|
|
|
|
display: inline-block;
|
|
|
|
font-size: 14px;
|
|
|
|
line-height: 50px;
|
|
|
|
margin-left: 8px;
|
|
|
|
.no-redirect {
|
|
|
|
color: #97a8be;
|
|
|
|
cursor: text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|