-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLuBreadCrumb.vue
More file actions
80 lines (72 loc) · 1.76 KB
/
Copy pathLuBreadCrumb.vue
File metadata and controls
80 lines (72 loc) · 1.76 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<template>
<div class="container pt-2 pt-lg-3 ">
<nav aria-label="breadcrumb">
<ol class="breadcrumb m-0 p-0 mb-0 font-size-sm bg-transparent">
<li
v-for="part in partsButLast"
:key="part.path"
class="breadcrumb-item text-truncate mb-0"
>
<router-link :to="part.path">
{{ part.label }}
</router-link>
</li>
<li
class="breadcrumb-item text-truncate mb-0 active"
aria-current="page"
>
{{ partsLast.label }}
</li>
</ol>
</nav>
</div>
</template>
<script setup>
import { computed, ref, watch } from 'vue'
import { useRoute, useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
const route = useRoute()
const router = useRouter()
const { t } = useI18n()
const parts = ref([])
const partsButLast = computed(() => {
if (!parts.value.length) {
return []
}
return parts.value.slice(0, -1)
})
const partsLast = computed(() => {
if (!parts.value.length) {
return { label: '' }
}
return parts.value[parts.value.length - 1]
})
const getLabel = (path) => {
const matchingRoute = router.getRoutes().find(r => r.path === path)
if (!matchingRoute) {
return path
}
const routeName = matchingRoute.name
if (typeof routeName === 'string') {
return t(routeName)
}
return path
}
const setPath = (path) => {
const paths = [{ path: '/', label: getLabel('/') }]
let accumulatedPath = ''
path.split('/').forEach(segment => {
if (!segment) { return }
accumulatedPath += `/${segment}`
paths.push({ path: accumulatedPath, label: getLabel(accumulatedPath) })
})
parts.value = paths
}
watch(
() => route.path,
(path) => {
setPath(path)
},
{ immediate: true },
)
</script>