perf: breadCrumb

This commit is contained in:
xiaoxian521 2021-12-17 14:46:30 +08:00
parent 86177e430e
commit ab93216dbe

View File

@ -1,13 +1,13 @@
<script setup lang="ts"> <script setup lang="ts">
import { ref, watch } from "vue"; import { ref, watch } from "vue";
import { isEqual } from "lodash-es";
import { transformI18n } from "/@/plugins/i18n"; import { transformI18n } from "/@/plugins/i18n";
import { isEqual, findIndex } from "lodash-es";
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
import { getParentPaths, findRouteByPath } from "/@/router/utils"; import { getParentPaths, findRouteByPath } from "/@/router/utils";
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
import { useRoute, useRouter, RouteLocationMatched } from "vue-router"; import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
const levelList = ref([]);
const route = useRoute(); const route = useRoute();
const levelList = ref([]);
const router = useRouter(); const router = useRouter();
const routes = router.options.routes; const routes = router.options.routes;
const multiTags = useMultiTagsStoreHook().multiTags; const multiTags = useMultiTagsStoreHook().multiTags;
@ -20,34 +20,20 @@ const isDashboard = (route: RouteLocationMatched): boolean | string => {
return name.trim().toLocaleLowerCase() === "welcome".toLocaleLowerCase(); return name.trim().toLocaleLowerCase() === "welcome".toLocaleLowerCase();
}; };
//
const getDynamicRoute = (path, tags) => {
const dynamicRoute = findRouteByPath(path, tags);
if (!dynamicRoute) {
return null;
} else if (isEqual(dynamicRoute.query, route.query)) {
return dynamicRoute;
} else {
const index = findIndex(
tags,
v => v.path === dynamicRoute.path && isEqual(v.query, dynamicRoute.query)
);
// query
const newTags = tags.slice(index + 1);
return getDynamicRoute(path, newTags);
}
};
const getBreadcrumb = (): void => { const getBreadcrumb = (): void => {
// //
let currentRoute; let currentRoute;
if (route.meta?.realPath) { if (Object.keys(route.query).length > 0) {
currentRoute = getDynamicRoute(route.path, multiTags); multiTags.forEach(item => {
if (isEqual(route.query, item?.query)) {
currentRoute = item;
}
});
} else { } else {
currentRoute = findRouteByPath(route.path, multiTags); currentRoute = findRouteByPath(router.currentRoute.value.path, multiTags);
} }
// //
const parentRoutes = getParentPaths(route.path, routes); const parentRoutes = getParentPaths(router.currentRoute.value.path, routes);
// //
let matched = []; let matched = [];
// //
@ -56,9 +42,12 @@ const getBreadcrumb = (): void => {
matched.push(findRouteByPath(path, routes)); matched.push(findRouteByPath(path, routes));
} }
}); });
if (route.meta.refreshRedirect) { if (router.currentRoute.value.meta?.refreshRedirect) {
matched.unshift( matched.unshift(
findRouteByPath(route.meta.refreshRedirect as string, routes) findRouteByPath(
router.currentRoute.value.meta.refreshRedirect as string,
routes
)
); );
} else { } else {
// //
@ -66,7 +55,7 @@ const getBreadcrumb = (): void => {
return !item.redirect || (item.redirect && item.children.length !== 1); return !item.redirect || (item.redirect && item.children.length !== 1);
}); });
} }
if (currentRoute.path !== "/welcome") { if (currentRoute?.path !== "/welcome") {
matched.push(currentRoute); matched.push(currentRoute);
} }
@ -82,7 +71,7 @@ const getBreadcrumb = (): void => {
} }
levelList.value = matched.filter( levelList.value = matched.filter(
item => item.meta && item.meta.title && item.meta.breadcrumb !== false item => item?.meta && item?.meta.title !== false
); );
}; };
@ -93,6 +82,11 @@ watch(
() => getBreadcrumb() () => getBreadcrumb()
); );
watch(
() => route.query,
() => getBreadcrumb()
);
const handleLink = (item: RouteLocationMatched): any => { const handleLink = (item: RouteLocationMatched): any => {
const { redirect, path } = item; const { redirect, path } = item;
if (redirect) { if (redirect) {