From 4c658f4868c7df6e0b8f18728c5d5ae53b04448a Mon Sep 17 00:00:00 2001 From: vben Date: Sun, 1 Nov 2020 11:55:18 +0800 Subject: [PATCH] perf: the routeModule can ignore the layou configuration without writing --- CHANGELOG.zh_CN.md | 1 + src/router/menus/index.ts | 9 +++++++-- src/router/types.d.ts | 2 +- src/utils/helper/menuHelper.ts | 8 ++++++-- src/utils/helper/routeHelper.ts | 16 +++++++++++----- 5 files changed, 26 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.zh_CN.md b/CHANGELOG.zh_CN.md index 9de39056..84a8968a 100644 --- a/CHANGELOG.zh_CN.md +++ b/CHANGELOG.zh_CN.md @@ -5,6 +5,7 @@ - Layout 界面布局样式调整 - 优化表格渲染性能 - 表单折叠搜索添图标添加动画 +- routeModule 可以忽略 layou 配置不写。方便配置一级菜单 ### 🐛 Bug Fixes diff --git a/src/router/menus/index.ts b/src/router/menus/index.ts index ebed4ce1..a5548cb1 100644 --- a/src/router/menus/index.ts +++ b/src/router/menus/index.ts @@ -87,8 +87,13 @@ export async function getFlatChildrenMenus(children: Menu[]) { function basicFilter(routes: RouteRecordNormalized[]) { return (menu: Menu) => { const matchRoute = routes.find((route) => { - if (route.meta && route.meta.carryParam) { - return pathToRegexp(route.path).test(menu.path); + if (route.meta) { + if (route.meta.carryParam) { + return pathToRegexp(route.path).test(menu.path); + } + if (route.meta.ignoreAuth) { + return false; + } } return route.path === menu.path; }); diff --git a/src/router/types.d.ts b/src/router/types.d.ts index 84969fc3..215a3c47 100644 --- a/src/router/types.d.ts +++ b/src/router/types.d.ts @@ -67,6 +67,6 @@ export interface MenuModule { } export interface AppRouteModule { - layout: AppRouteRecordRaw; + layout?: AppRouteRecordRaw; routes: AppRouteRecordRaw[]; } diff --git a/src/utils/helper/menuHelper.ts b/src/utils/helper/menuHelper.ts index 64fc146c..8925a9ed 100644 --- a/src/utils/helper/menuHelper.ts +++ b/src/utils/helper/menuHelper.ts @@ -49,8 +49,12 @@ export function transformRouteToMenu(routeModList: AppRouteModule[]) { const routeList: AppRouteRecordRaw[] = []; cloneRouteModList.forEach((item) => { const { layout, routes } = item; - layout.children = routes; - routeList.push(layout); + if (layout) { + layout.children = routes; + routeList.push(layout); + } else { + routeList.push(...routes); + } }); return treeMap(routeList, { conversion: (node: AppRouteRecordRaw) => { diff --git a/src/utils/helper/routeHelper.ts b/src/utils/helper/routeHelper.ts index 5014c9cc..3927d319 100644 --- a/src/utils/helper/routeHelper.ts +++ b/src/utils/helper/routeHelper.ts @@ -23,18 +23,24 @@ export function genRouteModule(moduleList: AppRouteModule[]) { for (const routeMod of moduleList) { const routes = routeMod.routes as any; const layout = routeMod.layout; - let router = createRouter({ routes, history: createWebHashHistory() }); + const router = createRouter({ routes, history: createWebHashHistory() }); - const flatList = toRaw(router.getRoutes()).filter((item) => item.children.length === 0); + const flatList = (toRaw(router.getRoutes()).filter( + (item) => item.children.length === 0 + ) as unknown) as AppRouteRecordRaw[]; try { (router as any) = null; } catch (error) {} flatList.forEach((item) => { - item.path = `${layout.path}${item.path}`; + item.path = `${layout ? layout.path : ''}${item.path}`; }); - layout.children = (flatList as unknown) as AppRouteRecordRaw[]; - ret.push(layout); + if (layout) { + layout.children = flatList; + ret.push(layout); + } else { + ret.push(...flatList); + } } return ret as RouteRecordRaw[]; } -- GitLab