forked from Tencent/tdesign-vue-next-starter
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBreadcrumb.vue
More file actions
51 lines (46 loc) · 1.35 KB
/
Breadcrumb.vue
File metadata and controls
51 lines (46 loc) · 1.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<template>
<t-breadcrumb :max-item-width="'150'" class="tdesign-breadcrumb">
<t-breadcrumbItem v-for="item in crumbs" :key="item.to" :to="item.to">
{{ item.title }}
</t-breadcrumbItem>
</t-breadcrumb>
</template>
<script setup lang="ts">
import { computed } from 'vue';
import { useRoute } from 'vue-router';
import { useLocale } from '@/locales/useLocale';
import { RouteMeta } from '@/types/interface';
const { locale } = useLocale();
const route = useRoute();
const crumbs = computed(() => {
const pathArray = route.path.split('/');
pathArray.shift();
const breadcrumbs = pathArray.reduce((breadcrumbArray, path, idx) => {
// 如果路由下有hiddenBreadcrumb或当前遍历到参数则隐藏
const meta = route.matched[idx]?.meta as RouteMeta;
if (meta?.hiddenBreadcrumb || Object.values(route.params).includes(path)) {
return breadcrumbArray;
}
let title = path;
if (meta?.title) {
if (typeof meta.title === 'string') {
title = meta.title;
} else {
title = meta.title[locale.value];
}
}
breadcrumbArray.push({
path,
to: breadcrumbArray[idx - 1] ? `${breadcrumbArray[idx - 1].to}/${path}` : `/${path}`,
title,
});
return breadcrumbArray;
}, []);
return breadcrumbs;
});
</script>
<style scoped>
.tdesign-breadcrumb {
margin-bottom: 24px;
}
</style>