perf: breadcrumb

This commit is contained in:
lrl 2021-12-17 09:40:16 +08:00 committed by 啝裳
parent 0903008ced
commit 93ac4fa813

View File

@ -1,11 +1,16 @@
<script setup lang="ts">
import { ref, watch } from "vue";
import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
import { transformI18n } from "/@/plugins/i18n";
import { isEmpty, isEqual, findIndex } from "lodash-es";
import { useMultiTagsStoreHook } from "/@/store/modules/multiTags";
import { getParentPaths, findRouteByPath } from "/@/router/utils";
import { useRoute, useRouter, RouteLocationMatched } from "vue-router";
const levelList = ref([]);
const route = useRoute();
const router = useRouter();
const routes = router.options.routes;
const multiTags = useMultiTagsStoreHook().multiTags;
const isDashboard = (route: RouteLocationMatched): boolean | string => {
const name = route && (route.name as string);
@ -15,8 +20,60 @@ const isDashboard = (route: RouteLocationMatched): boolean | string => {
return name.trim().toLocaleLowerCase() === "welcome".toLocaleLowerCase();
};
//
const getDynamicRoute = (path, tags) => {
const dynamicRoute = findRouteByPath(path, tags);
if (!dynamicRoute) {
return null;
} else if (isEmpty(route.query) || isEqual(dynamicRoute.query, route.query)) {
/**
* isEmpty(route.query) 动态标签页已存在切换标签页时不需要判断route.query
* isEqual(dynamicRoute.query, route.query) 新开动态标签页, 匹配与新开标签页相符的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 => {
let matched = route.matched.filter(item => item.meta && item.meta.title);
//
let currentRoute;
if (route.meta?.realPath) {
currentRoute = getDynamicRoute(route.path, multiTags);
} else {
currentRoute = findRouteByPath(route.path, multiTags);
}
//
const parentRoutes = getParentPaths(route.path, routes);
//
let matched = [];
//
parentRoutes.forEach(path => {
if (path !== "/") {
matched.push(findRouteByPath(path, routes));
}
});
if (route.meta.refreshRedirect) {
matched.unshift(
findRouteByPath(route.meta.refreshRedirect as string, routes)
);
} else {
//
matched = matched.filter(item => {
return !item.redirect || (item.redirect && item.children.length !== 1);
});
}
if (currentRoute.path !== "/welcome") {
matched.push(currentRoute);
}
const first = matched[0];
if (!isDashboard(first)) {
matched = [
@ -27,6 +84,7 @@ const getBreadcrumb = (): void => {
} as unknown as RouteLocationMatched
].concat(matched);
}
levelList.value = matched.filter(
item => item.meta && item.meta.title && item.meta.breadcrumb !== false
);