Skip to content

Commit d37e594

Browse files
committed
refactor: update breadcrumb;
;
1 parent dbe35e8 commit d37e594

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

src/components/AppBreadcrumb.vue

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<template>
2+
<CBreadcrumb class="d-md-down-none me-auto mb-0">
3+
<CBreadcrumbItem
4+
v-for="item in breadcrumbs"
5+
:href="item.active ? '' : item.path"
6+
:active="item.active"
7+
:key="item"
8+
>
9+
{{ item.name }}
10+
</CBreadcrumbItem>
11+
</CBreadcrumb>
12+
</template>
13+
14+
<script>
15+
import { onMounted, ref } from 'vue'
16+
import router from '@/router'
17+
18+
export default {
19+
name: 'AppBreadcrumb',
20+
setup() {
21+
const breadcrumbs = ref()
22+
23+
const getBreadcrumbs = () => {
24+
return router.currentRoute.value.matched.map((route) => {
25+
return {
26+
active: route.path === router.currentRoute.value.fullPath,
27+
name: route.name,
28+
path: `${router.options.history.base}${route.path}`,
29+
}
30+
})
31+
}
32+
33+
router.afterEach(() => {
34+
breadcrumbs.value = getBreadcrumbs()
35+
})
36+
37+
onMounted(() => {
38+
breadcrumbs.value = getBreadcrumbs()
39+
})
40+
41+
return {
42+
breadcrumbs,
43+
}
44+
},
45+
}
46+
</script>

src/components/AppHeader.vue

+3-47
Original file line numberDiff line numberDiff line change
@@ -39,68 +39,24 @@
3939
</CContainer>
4040
<CHeaderDivider />
4141
<CContainer fluid>
42-
<CBreadcrumb class="d-md-down-none me-auto mb-0">
43-
<CBreadcrumbItem
44-
v-for="item in breadcrumbs"
45-
:href="item.to"
46-
:active="item.to === '' ? true : false"
47-
:key="item"
48-
>
49-
{{ item.name }}
50-
</CBreadcrumbItem>
51-
</CBreadcrumb>
42+
<AppBreadcrumb />
5243
</CContainer>
5344
</CHeader>
5445
</template>
5546

5647
<script>
57-
import { onMounted, ref, watch } from 'vue'
58-
import { onBeforeRouteUpdate } from 'vue-router'
48+
import AppBreadcrumb from './AppBreadcrumb'
5949
import AppHeaderDropdownAccnt from './AppHeaderDropdownAccnt'
60-
import router from '@/router'
6150
import { logo } from '@/assets/brand/logo'
6251
export default {
6352
name: 'AppHeader',
6453
components: {
54+
AppBreadcrumb,
6555
AppHeaderDropdownAccnt,
6656
},
6757
setup() {
68-
const upperCaseFirstChar = (string) =>
69-
string.substr(0, 1).toUpperCase() + string.substr(1)
70-
71-
const makeCurrentRoute = () => {
72-
let result = [
73-
{ to: '/', name: 'Home'}
74-
]
75-
let path = router.currentRoute._value.path
76-
let temp = path.split('/')
77-
let to = ''
78-
if (temp.length <= 2) {
79-
result.push({ to: '', name: router.currentRoute._value.name })
80-
} else {
81-
for (let i = 1; i < temp.length - 1; i++) {
82-
for (let j = 1; j <= i; j++) {
83-
to += `/${temp[j]}`
84-
}
85-
result.push({ to: to, name: upperCaseFirstChar(temp[i]) })
86-
}
87-
result.push({ to: '', name: router.currentRoute._value.name })
88-
}
89-
return result
90-
}
91-
92-
const breadcrumbs = ref([])
93-
94-
onBeforeRouteUpdate(() => {
95-
breadcrumbs.value = makeCurrentRoute()
96-
})
97-
98-
onMounted(() => {
99-
breadcrumbs.value = makeCurrentRoute()
100-
})
10158
10259
return {
103-
breadcrumbs,
10460
logo,
10561
}
10662
},

0 commit comments

Comments
 (0)