Commit 4c658f4868c7df6e0b8f18728c5d5ae53b04448a
1 parent
b36d9486
perf: the routeModule can ignore the layou configuration without writing
Showing
5 changed files
with
26 additions
and
10 deletions
CHANGELOG.zh_CN.md
src/router/menus/index.ts
@@ -87,8 +87,13 @@ export async function getFlatChildrenMenus(children: Menu[]) { | @@ -87,8 +87,13 @@ export async function getFlatChildrenMenus(children: Menu[]) { | ||
87 | function basicFilter(routes: RouteRecordNormalized[]) { | 87 | function basicFilter(routes: RouteRecordNormalized[]) { |
88 | return (menu: Menu) => { | 88 | return (menu: Menu) => { |
89 | const matchRoute = routes.find((route) => { | 89 | const matchRoute = routes.find((route) => { |
90 | - if (route.meta && route.meta.carryParam) { | ||
91 | - return pathToRegexp(route.path).test(menu.path); | 90 | + if (route.meta) { |
91 | + if (route.meta.carryParam) { | ||
92 | + return pathToRegexp(route.path).test(menu.path); | ||
93 | + } | ||
94 | + if (route.meta.ignoreAuth) { | ||
95 | + return false; | ||
96 | + } | ||
92 | } | 97 | } |
93 | return route.path === menu.path; | 98 | return route.path === menu.path; |
94 | }); | 99 | }); |
src/router/types.d.ts
@@ -67,6 +67,6 @@ export interface MenuModule { | @@ -67,6 +67,6 @@ export interface MenuModule { | ||
67 | } | 67 | } |
68 | 68 | ||
69 | export interface AppRouteModule { | 69 | export interface AppRouteModule { |
70 | - layout: AppRouteRecordRaw; | 70 | + layout?: AppRouteRecordRaw; |
71 | routes: AppRouteRecordRaw[]; | 71 | routes: AppRouteRecordRaw[]; |
72 | } | 72 | } |
src/utils/helper/menuHelper.ts
@@ -49,8 +49,12 @@ export function transformRouteToMenu(routeModList: AppRouteModule[]) { | @@ -49,8 +49,12 @@ export function transformRouteToMenu(routeModList: AppRouteModule[]) { | ||
49 | const routeList: AppRouteRecordRaw[] = []; | 49 | const routeList: AppRouteRecordRaw[] = []; |
50 | cloneRouteModList.forEach((item) => { | 50 | cloneRouteModList.forEach((item) => { |
51 | const { layout, routes } = item; | 51 | const { layout, routes } = item; |
52 | - layout.children = routes; | ||
53 | - routeList.push(layout); | 52 | + if (layout) { |
53 | + layout.children = routes; | ||
54 | + routeList.push(layout); | ||
55 | + } else { | ||
56 | + routeList.push(...routes); | ||
57 | + } | ||
54 | }); | 58 | }); |
55 | return treeMap(routeList, { | 59 | return treeMap(routeList, { |
56 | conversion: (node: AppRouteRecordRaw) => { | 60 | conversion: (node: AppRouteRecordRaw) => { |
src/utils/helper/routeHelper.ts
@@ -23,18 +23,24 @@ export function genRouteModule(moduleList: AppRouteModule[]) { | @@ -23,18 +23,24 @@ export function genRouteModule(moduleList: AppRouteModule[]) { | ||
23 | for (const routeMod of moduleList) { | 23 | for (const routeMod of moduleList) { |
24 | const routes = routeMod.routes as any; | 24 | const routes = routeMod.routes as any; |
25 | const layout = routeMod.layout; | 25 | const layout = routeMod.layout; |
26 | - let router = createRouter({ routes, history: createWebHashHistory() }); | 26 | + const router = createRouter({ routes, history: createWebHashHistory() }); |
27 | 27 | ||
28 | - const flatList = toRaw(router.getRoutes()).filter((item) => item.children.length === 0); | 28 | + const flatList = (toRaw(router.getRoutes()).filter( |
29 | + (item) => item.children.length === 0 | ||
30 | + ) as unknown) as AppRouteRecordRaw[]; | ||
29 | try { | 31 | try { |
30 | (router as any) = null; | 32 | (router as any) = null; |
31 | } catch (error) {} | 33 | } catch (error) {} |
32 | 34 | ||
33 | flatList.forEach((item) => { | 35 | flatList.forEach((item) => { |
34 | - item.path = `${layout.path}${item.path}`; | 36 | + item.path = `${layout ? layout.path : ''}${item.path}`; |
35 | }); | 37 | }); |
36 | - layout.children = (flatList as unknown) as AppRouteRecordRaw[]; | ||
37 | - ret.push(layout); | 38 | + if (layout) { |
39 | + layout.children = flatList; | ||
40 | + ret.push(layout); | ||
41 | + } else { | ||
42 | + ret.push(...flatList); | ||
43 | + } | ||
38 | } | 44 | } |
39 | return ret as RouteRecordRaw[]; | 45 | return ret as RouteRecordRaw[]; |
40 | } | 46 | } |