perf: 代码优化 layout/Breadcrumb

This commit is contained in:
pany 2023-06-13 13:01:48 +08:00
parent 62e1a7a63d
commit 710ff1cda7

View File

@ -6,20 +6,21 @@ import { compile } from "path-to-regexp"
const route = useRoute() const route = useRoute()
const router = useRouter() const router = useRouter()
/** 定义响应式数据 breadcrumbs用于存储面包屑导航信息 */
const breadcrumbs = ref<RouteLocationMatched[]>([]) const breadcrumbs = ref<RouteLocationMatched[]>([])
/** 获取面包屑导航信息 */
const getBreadcrumb = () => { const getBreadcrumb = () => {
breadcrumbs.value = route.matched.filter((item) => { breadcrumbs.value = route.matched.filter((item) => item.meta?.title && item.meta?.breadcrumb !== false)
return item.meta && item.meta.title && item.meta.breadcrumb !== false
})
} }
/** 编译路由路径 */
const pathCompile = (path: string) => { const pathCompile = (path: string) => {
const { params } = route
const toPath = compile(path) const toPath = compile(path)
return toPath(params) return toPath(route.params)
} }
/** 处理面包屑导航点击事件 */
const handleLink = (item: RouteLocationMatched) => { const handleLink = (item: RouteLocationMatched) => {
const { redirect, path } = item const { redirect, path } = item
if (redirect) { if (redirect) {
@ -29,16 +30,16 @@ const handleLink = (item: RouteLocationMatched) => {
router.push(pathCompile(path)) router.push(pathCompile(path))
} }
/** 监听路由变化,更新面包屑导航信息 */
watch( watch(
() => route.path, () => route.path,
(path) => { (path) => {
if (path.startsWith("/redirect/")) { if (path.startsWith("/redirect/")) return
return
}
getBreadcrumb() getBreadcrumb()
} }
) )
/** 初始化面包屑导航信息 */
getBreadcrumb() getBreadcrumb()
</script> </script>