mirror of
				https://github.com/pure-admin/vue-pure-admin.git
				synced 2025-11-03 13:44:47 +08:00 
			
		
		
		
	perf: breadCrumb
This commit is contained in:
		
							parent
							
								
									86177e430e
								
							
						
					
					
						commit
						ab93216dbe
					
				@ -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) {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user