|
41 | 41 | <CContainer fluid> |
42 | 42 | <CBreadcrumb class="d-md-down-none me-auto mb-0"> |
43 | 43 | <CBreadcrumbItem |
44 | | - v-for="item in currentRoute" |
| 44 | + v-for="item in breadcrumbs" |
45 | 45 | :href="item.to" |
46 | 46 | :active="item.to === '' ? true : false" |
47 | 47 | :key="item" |
|
54 | 54 | </template> |
55 | 55 |
|
56 | 56 | <script> |
57 | | -import AppHeaderDropdownAccnt from "./AppHeaderDropdownAccnt"; |
58 | | -
|
59 | | -import router from "../router"; |
| 57 | +import { onMounted, ref, watch } from 'vue' |
| 58 | +import { onBeforeRouteUpdate } from 'vue-router' |
| 59 | +import AppHeaderDropdownAccnt from './AppHeaderDropdownAccnt' |
| 60 | +import router from '../router' |
60 | 61 | export default { |
61 | | - name: "AppHeader", |
| 62 | + name: 'AppHeader', |
62 | 63 | components: { |
63 | 64 | AppHeaderDropdownAccnt, |
64 | 65 | }, |
65 | | - data: function () { |
66 | | - return { |
67 | | - currentRoute: [], |
68 | | - }; |
69 | | - }, |
70 | | - methods: { |
71 | | - upperCaseFirstChar: function (string) { |
72 | | - return string.substr(0, 1).toUpperCase() + string.substr(1); |
73 | | - }, |
74 | | - makeCurrentRoute: function () { |
75 | | - let result = []; |
76 | | - let path = router.currentRoute._value.path; |
77 | | - let temp = path.split("/"); |
78 | | - let to = ""; |
| 66 | + setup() { |
| 67 | + const upperCaseFirstChar = (string) => |
| 68 | + string.substr(0, 1).toUpperCase() + string.substr(1) |
| 69 | +
|
| 70 | + const makeCurrentRoute = () => { |
| 71 | + let result = [ |
| 72 | + { to: '/', name: 'Home'} |
| 73 | + ] |
| 74 | + let path = router.currentRoute._value.path |
| 75 | + let temp = path.split('/') |
| 76 | + let to = '' |
79 | 77 | if (temp.length <= 2) { |
80 | | - result.push({ to: "", name: router.currentRoute._value.name }); |
| 78 | + result.push({ to: '', name: router.currentRoute._value.name }) |
81 | 79 | } else { |
82 | 80 | for (let i = 1; i < temp.length - 1; i++) { |
83 | 81 | for (let j = 1; j <= i; j++) { |
84 | | - to += `/${temp[j]}`; |
| 82 | + to += `/${temp[j]}` |
85 | 83 | } |
86 | | - result.push({ to: to, name: this.upperCaseFirstChar(temp[i]) }); |
| 84 | + result.push({ to: to, name: upperCaseFirstChar(temp[i]) }) |
87 | 85 | } |
88 | | - result.push({ to: "", name: router.currentRoute._value.name }); |
| 86 | + result.push({ to: '', name: router.currentRoute._value.name }) |
89 | 87 | } |
90 | | - return result; |
91 | | - }, |
92 | | - }, |
93 | | - watch: { |
94 | | - $route(to, from) { |
95 | | - this.currentRoute = this.makeCurrentRoute(); |
96 | | - }, |
97 | | - }, |
98 | | - mounted: function () { |
99 | | - this.currentRoute = this.makeCurrentRoute(); |
| 88 | + return result |
| 89 | + } |
| 90 | +
|
| 91 | + const breadcrumbs = ref([]) |
| 92 | +
|
| 93 | + onBeforeRouteUpdate(() => { |
| 94 | + breadcrumbs.value = makeCurrentRoute() |
| 95 | + }) |
| 96 | +
|
| 97 | + onMounted(() => { |
| 98 | + breadcrumbs.value = makeCurrentRoute() |
| 99 | + }) |
| 100 | +
|
| 101 | + return { |
| 102 | + breadcrumbs, |
| 103 | + } |
100 | 104 | }, |
101 | | -}; |
| 105 | +} |
102 | 106 | </script> |
0 commit comments