2022-04-21 18:20:39 +08:00
|
|
|
<script lang="ts" setup>
|
2022-08-23 11:13:55 +08:00
|
|
|
import { ref, watch } from "vue"
|
2022-10-18 15:07:42 +08:00
|
|
|
import { type RouteLocationMatched, useRoute, useRouter } from "vue-router"
|
2022-04-22 01:16:02 +08:00
|
|
|
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-08-23 11:13:55 +08:00
|
|
|
const breadcrumbs = ref<RouteLocationMatched[]>([])
|
|
|
|
|
|
|
|
const getBreadcrumb = () => {
|
|
|
|
breadcrumbs.value = route.matched.filter((item) => {
|
|
|
|
return item.meta && item.meta.title && item.meta.breadcrumb !== false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-08-23 11:13:55 +08:00
|
|
|
const handleLink = (item: RouteLocationMatched) => {
|
|
|
|
const { redirect, path } = item
|
|
|
|
if (redirect) {
|
2022-08-26 15:40:40 +08:00
|
|
|
router.push(redirect as string)
|
2022-08-23 11:13:55 +08:00
|
|
|
return
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
2022-08-26 15:40:40 +08:00
|
|
|
router.push(pathCompile(path))
|
2022-08-23 11:13:55 +08:00
|
|
|
}
|
2022-04-21 18:20:39 +08:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2022-08-23 11:13:55 +08:00
|
|
|
getBreadcrumb()
|
2022-04-21 18:20:39 +08:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2022-08-23 11:13:55 +08:00
|
|
|
getBreadcrumb()
|
2022-04-21 18:20:39 +08:00
|
|
|
</script>
|
|
|
|
|
2022-04-22 12:47:04 +08:00
|
|
|
<template>
|
|
|
|
<el-breadcrumb class="app-breadcrumb">
|
2022-08-25 17:11:34 +08:00
|
|
|
<el-breadcrumb-item v-for="(item, index) in breadcrumbs" :key="item.path">
|
|
|
|
<span v-if="item.redirect === 'noRedirect' || index === breadcrumbs.length - 1" class="no-redirect">
|
|
|
|
{{ item.meta.title }}
|
|
|
|
</span>
|
|
|
|
<a v-else @click.prevent="handleLink(item)">
|
|
|
|
{{ item.meta.title }}
|
|
|
|
</a>
|
|
|
|
</el-breadcrumb-item>
|
2022-04-22 12:47:04 +08:00
|
|
|
</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;
|
2022-05-25 21:53:38 +08:00
|
|
|
line-height: var(--v3-navigationbar-height);
|
2022-04-21 18:20:39 +08:00
|
|
|
margin-left: 8px;
|
|
|
|
.no-redirect {
|
|
|
|
color: #97a8be;
|
|
|
|
cursor: text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
</style>
|