mirror of
https://github.com/un-pany/v3-admin-vite.git
synced 2025-04-21 11:29:20 +08:00
refactor: BreadCrumb Component
This commit is contained in:
parent
487ceb002a
commit
0177b58f16
@ -1,38 +1,37 @@
|
||||
<script lang="ts" setup>
|
||||
import { onBeforeMount, reactive, watch } from "vue"
|
||||
import { ref, watch } from "vue"
|
||||
import { useRoute, useRouter, RouteLocationMatched } from "vue-router"
|
||||
import { compile } from "path-to-regexp"
|
||||
|
||||
const route = useRoute()
|
||||
const router = useRouter()
|
||||
|
||||
const breadcrumbs = ref<RouteLocationMatched[]>([])
|
||||
|
||||
const getBreadcrumb = () => {
|
||||
breadcrumbs.value = route.matched.filter((item) => {
|
||||
return item.meta && item.meta.title && item.meta.breadcrumb !== false
|
||||
})
|
||||
}
|
||||
|
||||
const pathCompile = (path: string) => {
|
||||
const { params } = route
|
||||
const toPath = compile(path)
|
||||
return toPath(params)
|
||||
}
|
||||
|
||||
const state = reactive({
|
||||
breadcrumbs: [] as Array<RouteLocationMatched>,
|
||||
getBreadcrumb: () => {
|
||||
const matched = route.matched.filter((item) => item.meta && item.meta.title)
|
||||
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) => {
|
||||
const handleLink = (item: RouteLocationMatched) => {
|
||||
const { redirect, path } = item
|
||||
if (redirect) {
|
||||
router.push(redirect as string).catch((err) => {
|
||||
console.warn(err)
|
||||
})
|
||||
return
|
||||
}
|
||||
})
|
||||
router.push(pathCompile(path)).catch((err) => {
|
||||
console.warn(err)
|
||||
})
|
||||
}
|
||||
|
||||
watch(
|
||||
() => route.path,
|
||||
@ -40,23 +39,21 @@ watch(
|
||||
if (path.startsWith("/redirect/")) {
|
||||
return
|
||||
}
|
||||
state.getBreadcrumb()
|
||||
getBreadcrumb()
|
||||
}
|
||||
)
|
||||
|
||||
onBeforeMount(() => {
|
||||
state.getBreadcrumb()
|
||||
})
|
||||
getBreadcrumb()
|
||||
</script>
|
||||
|
||||
<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)">
|
||||
<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>
|
||||
|
Loading…
x
Reference in New Issue
Block a user