Commit 9b61e82d7423d3d55c7cea34cc554495098bf1b9
1 parent
dc42d434
refactor: route Module structural transformation
Showing
9 changed files
with
57 additions
and
60 deletions
.env
... | ... | @@ -7,6 +7,5 @@ VITE_GLOB_APP_TITLE = Vben Admin |
7 | 7 | # spa shortname |
8 | 8 | VITE_GLOB_APP_SHORT_NAME = vue_vben_admin_2x |
9 | 9 | |
10 | -# Menu generation mode BACK|ROLE | |
11 | -# Need to delete LocalStorage after modification | |
12 | -VITE_GEN_MENU_MODE=ROLE | |
10 | +# Whether to dynamically load all files in `src/views` | |
11 | +VITE_DYNAMIC_IMPORT = true | ... | ... |
CHANGELOG.zh_CN.md
build/vite/plugin/dynamicImport/index.ts
... | ... | @@ -20,10 +20,11 @@ const dynamicImportTransform = function (env: any = {}): Transform { |
20 | 20 | return path.includes('/src/utils/helper/dynamicImport.ts'); |
21 | 21 | }, |
22 | 22 | transform({ code }) { |
23 | - const { VITE_GEN_MENU_MODE = '' } = env; | |
24 | - if (VITE_GEN_MENU_MODE !== 'BACK') { | |
23 | + const { VITE_DYNAMIC_IMPORT } = env; | |
24 | + if (!VITE_DYNAMIC_IMPORT) { | |
25 | 25 | return code; |
26 | 26 | } |
27 | + | |
27 | 28 | // if (!isBuild) return code; |
28 | 29 | // Only convert the dir |
29 | 30 | try { | ... | ... |
mock/sys/menu.ts
... | ... | @@ -2,17 +2,15 @@ import { resultSuccess } from '../_util'; |
2 | 2 | import { MockMethod } from 'vite-plugin-mock'; |
3 | 3 | |
4 | 4 | const dashboardRoute = { |
5 | - layout: { | |
6 | - path: '/dashboard', | |
7 | - name: 'Dashboard', | |
8 | - component: 'PAGE_LAYOUT', | |
9 | - redirect: '/dashboard/welcome', | |
10 | - meta: { | |
11 | - icon: 'ant-design:home-outlined', | |
12 | - title: 'Dashboard', | |
13 | - }, | |
5 | + path: '/dashboard', | |
6 | + name: 'Dashboard', | |
7 | + component: 'PAGE_LAYOUT', | |
8 | + redirect: '/dashboard/welcome', | |
9 | + meta: { | |
10 | + icon: 'ant-design:home-outlined', | |
11 | + title: 'Dashboard', | |
14 | 12 | }, |
15 | - routes: [ | |
13 | + children: [ | |
16 | 14 | { |
17 | 15 | path: '/welcome', |
18 | 16 | name: 'Welcome', |
... | ... | @@ -86,33 +84,27 @@ const backRoute = { |
86 | 84 | ], |
87 | 85 | }; |
88 | 86 | const authRoute = { |
89 | - layout: { | |
90 | - path: '/permission', | |
91 | - name: 'Permission', | |
92 | - component: 'PAGE_LAYOUT', | |
93 | - redirect: '/permission/front/page', | |
94 | - meta: { | |
95 | - icon: 'ant-design:home-outlined', | |
96 | - title: '权限管理', | |
97 | - }, | |
87 | + path: '/permission', | |
88 | + name: 'Permission', | |
89 | + component: 'PAGE_LAYOUT', | |
90 | + redirect: '/permission/front/page', | |
91 | + meta: { | |
92 | + icon: 'ant-design:home-outlined', | |
93 | + title: '权限管理', | |
98 | 94 | }, |
99 | - | |
100 | - routes: [frontRoute, backRoute], | |
95 | + children: [frontRoute, backRoute], | |
101 | 96 | }; |
102 | 97 | |
103 | 98 | const authRoute1 = { |
104 | - layout: { | |
105 | - path: '/permission', | |
106 | - name: 'Permission', | |
107 | - component: 'PAGE_LAYOUT', | |
108 | - redirect: '/permission/front/page', | |
109 | - meta: { | |
110 | - icon: 'ant-design:home-outlined', | |
111 | - title: '权限管理', | |
112 | - }, | |
99 | + path: '/permission', | |
100 | + name: 'Permission', | |
101 | + component: 'PAGE_LAYOUT', | |
102 | + redirect: '/permission/front/page', | |
103 | + meta: { | |
104 | + icon: 'ant-design:home-outlined', | |
105 | + title: '权限管理', | |
113 | 106 | }, |
114 | - | |
115 | - routes: [backRoute], | |
107 | + children: [backRoute], | |
116 | 108 | }; |
117 | 109 | export default [ |
118 | 110 | { | ... | ... |
src/router/types.d.ts
src/settings/projectSetting.ts
1 | 1 | import type { ProjectConfig } from '/@/types/config'; |
2 | 2 | |
3 | 3 | import { MenuTypeEnum, MenuThemeEnum, MenuModeEnum, TriggerEnum } from '/@/enums/menuEnum'; |
4 | -import { ContentEnum, RouterTransitionEnum } from '/@/enums/appEnum'; | |
4 | +import { ContentEnum, PermissionModeEnum, RouterTransitionEnum } from '/@/enums/appEnum'; | |
5 | 5 | import { primaryColor } from '../../build/config/lessModifyVars'; |
6 | -import { isProdMode, getRoleMode } from '/@/utils/env'; | |
6 | +import { isProdMode } from '/@/utils/env'; | |
7 | 7 | |
8 | 8 | // ! You need to clear the browser cache after the change |
9 | 9 | const setting: ProjectConfig = { |
10 | 10 | // Whether to show the configuration button |
11 | 11 | showSettingButton: true, |
12 | 12 | // 权限模式 |
13 | - permissionMode: getRoleMode(), | |
13 | + permissionMode: PermissionModeEnum.ROLE, | |
14 | 14 | // 网站灰色模式,用于可能悼念的日期开启 |
15 | 15 | grayMode: false, |
16 | 16 | // 色弱模式 | ... | ... |
src/utils/env.ts
1 | -import { PermissionModeEnum } from '../enums/appEnum'; | |
2 | 1 | import type { GlobEnvConfig } from '/@/types/config'; |
3 | 2 | |
4 | 3 | export const getGlobEnvConfig = (): GlobEnvConfig => { |
... | ... | @@ -47,14 +46,3 @@ export const isProdMode = (): boolean => import.meta.env.PROD; |
47 | 46 | * @example: |
48 | 47 | */ |
49 | 48 | export const isUseMock = (): boolean => import.meta.env.VITE_USE_MOCK === 'true'; |
50 | - | |
51 | -/** | |
52 | - * @description: 获取菜单生成方式 | |
53 | - * @param {type} | |
54 | - * @returns: | |
55 | - * @example: | |
56 | - */ | |
57 | -export const getRoleMode = (): PermissionModeEnum => | |
58 | - import.meta.env.VITE_GEN_MENU_MODE === PermissionModeEnum.ROLE | |
59 | - ? PermissionModeEnum.ROLE | |
60 | - : PermissionModeEnum.BACK; | ... | ... |
src/utils/helper/menuHelper.ts
... | ... | @@ -48,12 +48,12 @@ export function transformRouteToMenu(routeModList: AppRouteModule[]) { |
48 | 48 | const cloneRouteModList = cloneDeep(routeModList); |
49 | 49 | const routeList: AppRouteRecordRaw[] = []; |
50 | 50 | cloneRouteModList.forEach((item) => { |
51 | - const { layout, routes } = item; | |
51 | + const { layout, routes, children } = item; | |
52 | 52 | if (layout) { |
53 | - layout.children = routes; | |
53 | + layout.children = routes || children; | |
54 | 54 | routeList.push(layout); |
55 | 55 | } else { |
56 | - routeList.push(...routes); | |
56 | + routes && routeList.push(...routes); | |
57 | 57 | } |
58 | 58 | }); |
59 | 59 | return treeMap(routeList, { | ... | ... |
src/utils/helper/routeHelper.ts
... | ... | @@ -8,6 +8,7 @@ import { toRaw } from 'vue'; |
8 | 8 | import { PAGE_LAYOUT_COMPONENT } from '/@/router/constant'; |
9 | 9 | // import { isDevMode } from '/@/utils/env'; |
10 | 10 | import dynamicImport from './dynamicImport'; |
11 | +import { omit } from 'lodash-es'; | |
11 | 12 | |
12 | 13 | let currentTo: RouteLocationNormalized | null = null; |
13 | 14 | |
... | ... | @@ -23,8 +24,16 @@ export function setCurrentTo(to: RouteLocationNormalized) { |
23 | 24 | export function genRouteModule(moduleList: AppRouteModule[]) { |
24 | 25 | const ret: AppRouteRecordRaw[] = []; |
25 | 26 | for (const routeMod of moduleList) { |
26 | - const routes = routeMod.routes as any; | |
27 | - const layout = routeMod.layout; | |
27 | + let routes = []; | |
28 | + let layout: AppRouteRecordRaw | undefined; | |
29 | + if (Reflect.has(routeMod, 'routes')) { | |
30 | + routes = routeMod.routes as any; | |
31 | + layout = routeMod.layout; | |
32 | + } else if (Reflect.has(routeMod, 'path')) { | |
33 | + layout = omit(routeMod, 'children') as any; | |
34 | + routes = routeMod.children || []; | |
35 | + } | |
36 | + | |
28 | 37 | const router = createRouter({ routes, history: createWebHashHistory() }); |
29 | 38 | |
30 | 39 | const flatList = (toRaw(router.getRoutes()).filter( |
... | ... | @@ -45,7 +54,8 @@ export function genRouteModule(moduleList: AppRouteModule[]) { |
45 | 54 | |
46 | 55 | // 动态引入 |
47 | 56 | // TODO 错误写法 |
48 | -function asyncImportRoute(routes: AppRouteRecordRaw[]) { | |
57 | +function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) { | |
58 | + if (!routes) return; | |
49 | 59 | routes.forEach((item) => { |
50 | 60 | const { component } = item; |
51 | 61 | const { children } = item; |
... | ... | @@ -60,10 +70,14 @@ function asyncImportRoute(routes: AppRouteRecordRaw[]) { |
60 | 70 | // 将后台对象转成路由对象 |
61 | 71 | export function transformObjToRoute(routeList: AppRouteModule[]) { |
62 | 72 | routeList.forEach((route) => { |
63 | - asyncImportRoute(route.routes); | |
73 | + asyncImportRoute(Reflect.has(route, 'routes') ? route.routes : route.children || []); | |
64 | 74 | if (route.layout) { |
65 | 75 | route.layout.component = |
66 | 76 | route.layout.component === 'PAGE_LAYOUT' ? PAGE_LAYOUT_COMPONENT : ''; |
77 | + } else { | |
78 | + route.component = route.component === 'PAGE_LAYOUT' ? PAGE_LAYOUT_COMPONENT : ''; | |
79 | + const _layout = omit(route, 'children') as any; | |
80 | + route.layout = _layout; | |
67 | 81 | } |
68 | 82 | }); |
69 | 83 | return routeList; | ... | ... |