Skip to content

Commit

Permalink
perf: the routeModule can ignore the layou configuration without writing
Browse files Browse the repository at this point in the history
  • Loading branch information
anncwb committed Nov 1, 2020
1 parent b36d948 commit 4c658f4
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- Layout 界面布局样式调整
- 优化表格渲染性能
- 表单折叠搜索添图标添加动画
- routeModule 可以忽略 layou 配置不写。方便配置一级菜单

### 🐛 Bug Fixes

Expand Down
9 changes: 7 additions & 2 deletions src/router/menus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
});
Expand Down
2 changes: 1 addition & 1 deletion src/router/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@ export interface MenuModule {
}

export interface AppRouteModule {
layout: AppRouteRecordRaw;
layout?: AppRouteRecordRaw;
routes: AppRouteRecordRaw[];
}
8 changes: 6 additions & 2 deletions src/utils/helper/menuHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
16 changes: 11 additions & 5 deletions src/utils/helper/routeHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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[];
}
Expand Down

0 comments on commit 4c658f4

Please sign in to comment.