Browse Source

提交相关代码

Lijy 3 tháng trước cách đây
mục cha
commit
6851156068

+ 31 - 39
fhKeeper/formulahousekeeper/customerBuler-crm/src/router/routerGuards.ts

@@ -18,65 +18,57 @@ export function createRouterGuards(router: Router) {
           next();
         } else {
           setAsyncRoutesMark(true);
-          const newRouters: any = routers;
-          const addNewRouter = newRouters.find(
-            (item: any) => item.path == "/home"
-          );
-
-          let modules = import.meta.glob("@/pages/**/*.vue");
-          routerList.forEach((item: any, index: number) => {
-            let filePath = item.path.replace("/", "")
-            const children = item.children;
-            if (children && children.length > 0) {
-              children.forEach((child: any) => {
+          const modules = import.meta.glob("@/pages/**/*.vue");
+      
+          // 遍历菜单,动态添加到 'home' 路由下
+          routerList.forEach((item: any) => {
+            let filePath = item.path.replace("/", "");
+      
+            if (item.children && item.children.length > 0) {
+              item.children.forEach((child: any) => {
                 let childFilePath = child.path.replace("/", "");
-                addNewRouter?.children.push({
+                router.addRoute('home', {
                   path: child.path,
                   name: child.name,
                   meta: {},
-                  component: modules[`/src/pages/${childFilePath}/index.vue`]
+                  component: modules[`/src/pages/${childFilePath}/index.vue`],
                 });
               });
             } else {
-              addNewRouter?.children.push({
-                path: item.path,
-                name: item.name,
-                meta: {},
-                component: modules[`/src/pages/${filePath}/index.vue`],
-                redirect: item.path === '/biReport' ? `/biReport/cusReportForm` : ''
-              });
-              if(item.childrenList && item.childrenList.length > 0) {
-                addNewRouter.children[index + 1].children = item.childrenList.map((child: any) => {
+              let childRoutes: any[] = [];
+              if (item.childrenList && item.childrenList.length > 0) {
+                childRoutes = item.childrenList.map((child: any) => {
                   let childFilePath = child.path.replace("/", "");
                   return {
                     path: child.path,
                     name: child.name,
                     meta: { parentPath: item.path },
-                    component: modules[`/src/pages/${childFilePath}/index.vue`]
-                  }
-                })
-              }
-            }
-
-            if(item.childrenList && item.childrenList.length > 0) {
-              item.childrenList.forEach((child: any) => {
-                let childFilePath = child.path.replace("/", "");
-                addNewRouter?.children.push({
-                  path: child.path,
-                  name: child.name,
-                  meta: {},
-                  component: modules[`/src/pages/${childFilePath}/index.vue`]
+                    component: modules[`/src/pages/${childFilePath}/index.vue`],
+                  };
                 });
+              }
+      
+              router.addRoute('home', {
+                path: item.path,
+                name: item.name,
+                meta: {},
+                component: modules[`/src/pages/${filePath}/index.vue`],
+                redirect: item.path === '/biReport' ? `/biReport/cusReportForm` : '',
+                children: childRoutes
               });
             }
           });
-          router.addRoute(addNewRouter);
+      
+          // 添加404兜底
           router.addRoute({
             path: '/:catchAll(.*)',
             name: 'NotFound',
             component: () => import("../pages/404.vue"),
-          })
-          console.log(router.getRoutes(), '<==== router.getRoutes()')
+          });
+      
+          console.log(router.getRoutes(), '<==== router.getRoutes()');
+      
+          // ✅ 路由添加完毕后,重新进入当前页面,确保路由匹配
           next({ ...to, replace: true });
         }
       } else {