页面路径完善
This commit is contained in:
@@ -9,9 +9,46 @@ import ParentView from '@/components/ParentView/index.vue';
|
||||
import InnerLink from '@/layout/components/InnerLink/index.vue';
|
||||
import { ref } from 'vue';
|
||||
import { createCustomNameComponent } from '@/utils/createCustomNameComponent';
|
||||
import { ElNotification } from 'element-plus'; // 确保引入了这个
|
||||
|
||||
// 匹配views里面所有的.vue文件
|
||||
const modules = import.meta.glob('./../../views/**/*.vue');
|
||||
|
||||
const transformRoutes = (backendRoutes: any[]): any[] => {
|
||||
// 1. 深拷贝
|
||||
const processedRoutes = JSON.parse(JSON.stringify(backendRoutes));
|
||||
|
||||
// 2. 创建2级路由映射表(key: activeMenu, value: 标题)
|
||||
const level2TitleMap = new Map();
|
||||
|
||||
processedRoutes.forEach((level1Route: any) => {
|
||||
if (!level1Route.children) return;
|
||||
|
||||
level1Route.children.forEach((level2Route: any) => {
|
||||
if (!level2Route.hidden) {
|
||||
const fullPath = level1Route.path.endsWith('/') ? `${level1Route.path}${level2Route.path}` : `${level1Route.path}/${level2Route.path}`;
|
||||
level2TitleMap.set(fullPath, level2Route.meta?.title);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
// 3. 给三级路由的 meta 加 parentTitle(不做嵌套,保持扁平)
|
||||
processedRoutes.forEach((level1Route: any) => {
|
||||
if (!level1Route.children) return;
|
||||
|
||||
level1Route.children.forEach((route: any) => {
|
||||
if (route.hidden && route.meta?.activeMenu) {
|
||||
const parentTitle = level2TitleMap.get(route.meta.activeMenu);
|
||||
if (parentTitle) {
|
||||
// 🔥 关键:只加 parentTitle,不修改 children
|
||||
route.meta.parentTitle = parentTitle;
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
return processedRoutes;
|
||||
};
|
||||
export const usePermissionStore = defineStore('permission', () => {
|
||||
const routes = ref<RouteRecordRaw[]>([]);
|
||||
const addRoutes = ref<RouteRecordRaw[]>([]);
|
||||
@@ -45,16 +82,31 @@ export const usePermissionStore = defineStore('permission', () => {
|
||||
const setSidebarRouters = (routes: RouteRecordRaw[]): void => {
|
||||
sidebarRouters.value = routes;
|
||||
};
|
||||
|
||||
// --------------------------
|
||||
// 修改:generateRoutes 函数,加入转换逻辑
|
||||
// --------------------------
|
||||
const generateRoutes = async (): Promise<RouteRecordRaw[]> => {
|
||||
const res = await getRouters();
|
||||
const { data } = res;
|
||||
const sdata = JSON.parse(JSON.stringify(data));
|
||||
const rdata = JSON.parse(JSON.stringify(data));
|
||||
const defaultData = JSON.parse(JSON.stringify(data));
|
||||
console.log('后端原始路由:', data);
|
||||
|
||||
// 🔥 关键:先把后端扁平路由转成嵌套格式
|
||||
const nestedData = transformRoutes(data);
|
||||
console.log('转换后的嵌套路由:', nestedData);
|
||||
|
||||
// 深拷贝数据(保留原有逻辑)
|
||||
const sdata = JSON.parse(JSON.stringify(nestedData));
|
||||
const rdata = JSON.parse(JSON.stringify(nestedData));
|
||||
const defaultData = JSON.parse(JSON.stringify(nestedData));
|
||||
|
||||
// 后续使用转换后的 nestedData
|
||||
const sidebarRoutes = filterAsyncRouter(sdata);
|
||||
const rewriteRoutes = filterAsyncRouter(rdata, undefined, true);
|
||||
const defaultRoutes = filterAsyncRouter(defaultData);
|
||||
|
||||
const asyncRoutes = filterDynamicRoutes(dynamicRoutes);
|
||||
|
||||
asyncRoutes.forEach((route) => {
|
||||
router.addRoute(route);
|
||||
});
|
||||
@@ -191,7 +243,7 @@ function duplicateRouteChecker(localRoutes: Route[], routes: Route[]) {
|
||||
|
||||
const nameList: string[] = [];
|
||||
allRoutes.forEach((route) => {
|
||||
const name = route.name.toString();
|
||||
const name = route.name?.toString();
|
||||
if (name && nameList.includes(name)) {
|
||||
const message = `路由名称: [${name}] 重复, 会造成 404`;
|
||||
console.error(message);
|
||||
@@ -202,6 +254,8 @@ function duplicateRouteChecker(localRoutes: Route[], routes: Route[]) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
nameList.push(route.name.toString());
|
||||
if (name) {
|
||||
nameList.push(name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user