vben
authored
|
1
|
<template>
|
vben
authored
|
2
|
<div :class="[prefixCls, `${prefixCls}--${theme}`]">
|
vben
authored
|
3
|
<a-breadcrumb :routes="routes">
|
vben
authored
|
4
|
<template #itemRender="{ route, routes, paths }">
|
vben
authored
|
5
|
<Icon :icon="route.meta.icon" v-if="getShowBreadCrumbIcon && route.meta.icon" />
|
vben
authored
|
6
|
<span v-if="!hasRedirect(routes, route)">
|
vben
authored
|
7
8
|
{{ t(route.meta.title) }}
</span>
|
vben
authored
|
9
|
<router-link v-else to="" @click="handleClick(route, paths, $event)">
|
vben
authored
|
10
11
12
13
14
15
16
|
{{ t(route.meta.title) }}
</router-link>
</template>
</a-breadcrumb>
</div>
</template>
<script lang="ts">
|
vben
authored
|
17
18
|
import type { RouteLocationMatched } from 'vue-router';
|
vben
authored
|
19
|
import { defineComponent, ref, toRaw, watchEffect } from 'vue';
|
vben
authored
|
20
|
import { Breadcrumb } from 'ant-design-vue';
|
vben
authored
|
21
22
23
24
25
26
27
|
import { useRouter } from 'vue-router';
import { filter } from '/@/utils/helper/treeHelper';
import { REDIRECT_NAME } from '/@/router/constant';
import Icon from '/@/components/Icon';
import { PageEnum } from '/@/enums/pageEnum';
|
vben
authored
|
28
29
30
31
32
|
import { useDesign } from '/@/hooks/web/useDesign';
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { propTypes } from '/@/utils/propTypes';
|
vben
authored
|
33
34
|
import { useGo } from '/@/hooks/web/usePage';
import { isString } from '/@/utils/is';
|
Vben
authored
|
35
|
import { useI18n } from '/@/hooks/web/useI18n';
|
vben
authored
|
36
|
|
vben
authored
|
37
38
|
export default defineComponent({
name: 'LayoutBreadcrumb',
|
vben
authored
|
39
|
components: { Icon, [Breadcrumb.name]: Breadcrumb },
|
vben
authored
|
40
|
props: {
|
vben
authored
|
41
|
theme: propTypes.oneOf(['dark', 'light']),
|
vben
authored
|
42
43
44
45
|
},
setup() {
const routes = ref<RouteLocationMatched[]>([]);
const { currentRoute } = useRouter();
|
vben
authored
|
46
47
|
const { prefixCls } = useDesign('layout-breadcrumb');
const { getShowBreadCrumbIcon } = useRootSetting();
|
vben
authored
|
48
49
50
|
const { t } = useI18n();
watchEffect(() => {
|
vben
authored
|
51
52
|
if (currentRoute.value.name === REDIRECT_NAME) return;
|
vben
authored
|
53
|
const matched = currentRoute.value?.matched;
|
vben
authored
|
54
55
|
if (!matched || matched.length === 0) return;
|
vben
authored
|
56
|
let breadcrumbList = filterItem(toRaw(matched));
|
vben
authored
|
57
58
59
60
61
62
|
const filterBreadcrumbList = breadcrumbList.filter(
(item) => item.path !== PageEnum.BASE_HOME
);
if (filterBreadcrumbList.length === breadcrumbList.length) {
|
vben
authored
|
63
|
filterBreadcrumbList.unshift(({
|
vben
authored
|
64
65
66
|
path: PageEnum.BASE_HOME,
meta: {
title: t('layout.header.home'),
|
vben
authored
|
67
|
isLink: true,
|
vben
authored
|
68
|
},
|
vben
authored
|
69
|
} as unknown) as RouteLocationMatched);
|
vben
authored
|
70
|
}
|
vben
authored
|
71
72
73
74
|
if (currentRoute.value.meta?.currentActiveMenu) {
filterBreadcrumbList.push((currentRoute.value as unknown) as RouteLocationMatched);
}
|
Vben
authored
|
75
|
routes.value = subRouteExtraction(filterBreadcrumbList);
|
vben
authored
|
76
77
|
});
|
Vben
authored
|
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
|
function subRouteExtraction(routeList: RouteLocationMatched[]) {
const resultRoutes: RouteLocationMatched[] = [];
routeList.forEach((route) => {
if (route.children?.length === 1) {
const subRoute = route.children[0] as RouteLocationMatched;
const subRouteName = subRoute.name as string;
const routeName = route.name;
if (subRouteName && `${subRouteName}Parent` === routeName) {
route = subRoute;
}
}
resultRoutes.push(route);
});
return resultRoutes;
}
|
vben
authored
|
94
95
96
97
98
99
100
|
function filterItem(list: RouteLocationMatched[]) {
let resultList = filter(list, (item) => {
const { meta } = item;
if (!meta) {
return false;
}
|
Vben
authored
|
101
|
|
vben
authored
|
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
const { title, hideBreadcrumb, hideMenu } = meta;
if (!title || hideBreadcrumb || hideMenu) {
return false;
}
return true;
}).filter((item) => !item.meta?.hideBreadcrumb || !item.meta?.hideMenu);
return resultList;
}
function handleClick(route: RouteLocationMatched, paths: string[], e: Event) {
e?.preventDefault();
const {
children,
redirect,
meta,
|
Vben
authored
|
118
119
|
// components
|
vben
authored
|
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
} = route;
// const isParent =
// components?.default?.name === 'DefaultLayout' || (components?.default as any)?.parentView;
if (
children?.length &&
!redirect
// && !isParent
) {
e?.stopPropagation();
return;
}
if (meta?.carryParam) {
return;
}
const go = useGo();
if (redirect && isString(redirect)) {
go(redirect);
} else {
|
Vben
authored
|
141
142
143
144
145
146
147
148
149
150
151
|
let goPath = '';
if (paths.length === 1) {
goPath = paths[0];
} else {
const ps = paths.slice(1);
const lastPath = ps.pop() || '';
const parentPath = ps.pop() || '';
goPath = `${parentPath}/${lastPath}`;
}
goPath = /^\//.test(goPath) ? goPath : `/${goPath}`;
go(goPath);
|
vben
authored
|
152
153
154
155
156
157
158
|
}
}
function hasRedirect(routes: RouteLocationMatched[], route: RouteLocationMatched) {
if (routes.indexOf(route) === routes.length - 1) {
return false;
}
|
Vben
authored
|
159
160
161
162
163
|
// if (route?.meta?.isLink) {
// return true;
// }
|
vben
authored
|
164
165
166
167
|
return true;
}
return { routes, t, prefixCls, getShowBreadCrumbIcon, handleClick, hasRedirect };
|
vben
authored
|
168
169
170
|
},
});
</script>
|
vben
authored
|
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
<style lang="less">
@prefix-cls: ~'@{namespace}-layout-breadcrumb';
.@{prefix-cls} {
display: flex;
padding: 0 8px;
align-items: center;
.ant-breadcrumb-link {
.anticon {
margin-right: 4px;
margin-bottom: 2px;
}
}
&--light {
.ant-breadcrumb-link {
color: @breadcrumb-item-normal-color;
a {
|
vben
authored
|
191
|
color: rgba(0, 0, 0, 0.65);
|
vben
authored
|
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
&:hover {
color: @primary-color;
}
}
}
.ant-breadcrumb-separator {
color: @breadcrumb-item-normal-color;
}
}
&--dark {
.ant-breadcrumb-link {
color: rgba(255, 255, 255, 0.6);
a {
color: rgba(255, 255, 255, 0.8);
&:hover {
color: @white;
}
}
}
.ant-breadcrumb-separator,
.anticon {
color: rgba(255, 255, 255, 0.8);
}
}
}
</style>
|