67 lines
1.8 KiB
Vue
Raw Normal View History

<script lang="ts" setup>
2024-11-18 19:40:44 +08:00
import type { RouteLocationMatched } from "vue-router"
import { useRouteListener } from "@/hooks/useRouteListener"
import { compile } from "path-to-regexp"
2024-11-18 19:40:44 +08:00
import { ref } from "vue"
import { useRoute, useRouter } from "vue-router"
const route = useRoute()
const router = useRouter()
const { listenerRouteChange } = useRouteListener()
2023-06-13 13:01:48 +08:00
/** 定义响应式数据 breadcrumbs用于存储面包屑导航信息 */
2022-08-23 11:13:55 +08:00
const breadcrumbs = ref<RouteLocationMatched[]>([])
2023-06-13 13:01:48 +08:00
/** 获取面包屑导航信息 */
2024-11-18 19:40:44 +08:00
function getBreadcrumb() {
breadcrumbs.value = route.matched.filter(item => item.meta?.title && item.meta?.breadcrumb !== false)
2022-08-23 11:13:55 +08:00
}
2023-06-13 13:01:48 +08:00
/** 编译路由路径 */
2024-11-18 19:40:44 +08:00
function pathCompile(path: string) {
const toPath = compile(path)
2023-06-13 13:01:48 +08:00
return toPath(route.params)
}
2023-06-13 13:01:48 +08:00
/** 处理面包屑导航点击事件 */
2024-11-18 19:40:44 +08:00
function handleLink(item: RouteLocationMatched) {
2022-08-23 11:13:55 +08:00
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-08-26 15:40:40 +08:00
router.push(pathCompile(path))
2022-08-23 11:13:55 +08:00
}
2023-06-13 13:01:48 +08:00
/** 监听路由变化,更新面包屑导航信息 */
listenerRouteChange((route) => {
if (route.path.startsWith("/redirect/")) return
getBreadcrumb()
}, true)
</script>
<template>
2024-03-28 21:34:24 +08:00
<el-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>
</el-breadcrumb>
</template>
<style lang="scss" scoped>
2024-03-28 21:34:24 +08:00
.el-breadcrumb {
2022-05-25 21:53:38 +08:00
line-height: var(--v3-navigationbar-height);
.no-redirect {
2024-03-28 21:34:24 +08:00
color: var(--el-text-color-placeholder);
}
a {
font-weight: normal;
}
}
</style>