diff --git a/.eslintrc.js b/.eslintrc.js
index 23f997c5..8a1472aa 100644
--- a/.eslintrc.js
+++ b/.eslintrc.js
@@ -38,15 +38,15 @@ module.exports = {
'@typescript-eslint/no-unused-vars': [
'error',
{
- argsIgnorePattern: '^h$',
- varsIgnorePattern: '^h$',
+ argsIgnorePattern: '^_',
+ varsIgnorePattern: '^_',
},
],
'no-unused-vars': [
'error',
{
- argsIgnorePattern: '^h$',
- varsIgnorePattern: '^h$',
+ argsIgnorePattern: '^_',
+ varsIgnorePattern: '^_',
},
],
'space-before-function-paren': 'off',
diff --git a/.vscode/settings.json b/.vscode/settings.json
index a51df87b..f8ccecb0 100644
--- a/.vscode/settings.json
+++ b/.vscode/settings.json
@@ -8,7 +8,6 @@
"explorer.openEditors.visible": 0,
"editor.tabSize": 2,
"editor.renderControlCharacters": true,
- "window.zoomLevel": -1,
"editor.minimap.renderCharacters": false,
"editor.minimap.maxColumn": 300,
"editor.minimap.showSlider": "always",
diff --git a/CHANGELOG.zh_CN.md b/CHANGELOG.zh_CN.md
index be23fc6e..9092b4f6 100644
--- a/CHANGELOG.zh_CN.md
+++ b/CHANGELOG.zh_CN.md
@@ -1,3 +1,9 @@
+## Wip
+
+### ✨ Refactor
+
+- 重构路由多层模式,解决嵌套 keepalive 执行多次问题
+
## 2.1.0 (2021-03-15)
### ✨ Features
diff --git a/build/vite/plugin/hmr.ts b/build/vite/plugin/hmr.ts
new file mode 100644
index 00000000..cb84e264
--- /dev/null
+++ b/build/vite/plugin/hmr.ts
@@ -0,0 +1,21 @@
+import type { Plugin } from 'vite';
+
+/**
+ * TODO
+ * Temporarily solve the Vite circular dependency problem, and wait for a better solution to fix it later. I don't know what problems this writing will bring.
+ * @returns
+ */
+
+export function configHmrPlugin(): Plugin {
+ return {
+ name: 'singleHMR',
+ handleHotUpdate({ modules, file }) {
+ if (file.match(/xml$/)) return [];
+ modules.forEach((m) => {
+ m.importedModules = new Set();
+ m.importers = new Set();
+ });
+ return modules;
+ },
+ };
+}
diff --git a/build/vite/plugin/index.ts b/build/vite/plugin/index.ts
index cf0ce0d6..d34cf049 100644
--- a/build/vite/plugin/index.ts
+++ b/build/vite/plugin/index.ts
@@ -17,6 +17,7 @@ import { configThemePlugin } from './theme';
import { configImageminPlugin } from './imagemin';
import { configWindiCssPlugin } from './windicss';
import { configSvgIconsPlugin } from './svgSprite';
+import { configHmrPlugin } from './hmr';
export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
const { VITE_USE_IMAGEMIN, VITE_USE_MOCK, VITE_LEGACY, VITE_BUILD_COMPRESS } = viteEnv;
@@ -28,6 +29,9 @@ export function createVitePlugins(viteEnv: ViteEnv, isBuild: boolean) {
vueJsx(),
];
+ // TODO
+ !isBuild && vitePlugins.push(configHmrPlugin());
+
// @vitejs/plugin-legacy
VITE_LEGACY && isBuild && vitePlugins.push(legacy());
diff --git a/src/layouts/default/header/components/Breadcrumb.vue b/src/layouts/default/header/components/Breadcrumb.vue
index 6ec6480e..d413400b 100644
--- a/src/layouts/default/header/components/Breadcrumb.vue
+++ b/src/layouts/default/header/components/Breadcrumb.vue
@@ -33,6 +33,7 @@
import { useGo } from '/@/hooks/web/usePage';
import { isString } from '/@/utils/is';
import { useI18n } from '/@/hooks/web/useI18n';
+ import { getMenus } from '/@/router/menus';
export default defineComponent({
name: 'LayoutBreadcrumb',
@@ -47,7 +48,7 @@
const { getShowBreadCrumbIcon } = useRootSetting();
const { t } = useI18n();
- watchEffect(() => {
+ watchEffect(async () => {
if (currentRoute.value.name === REDIRECT_NAME) return;
const matched = currentRoute.value?.matched;
diff --git a/src/layouts/page/ParentView.vue b/src/layouts/page/ParentView.vue
deleted file mode 100644
index 246beeaa..00000000
--- a/src/layouts/page/ParentView.vue
+++ /dev/null
@@ -1,63 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
diff --git a/src/layouts/page/index.vue b/src/layouts/page/index.vue
index f8a03ea0..096d84da 100644
--- a/src/layouts/page/index.vue
+++ b/src/layouts/page/index.vue
@@ -16,9 +16,9 @@
appear
>
-
+
-
+
@@ -34,15 +34,15 @@
import { useRootSetting } from '/@/hooks/setting/useRootSetting';
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
- import { useCache, getKey } from './useCache';
import { useMultipleTabSetting } from '/@/hooks/setting/useMultipleTabSetting';
import { getTransitionName } from './transition';
+ import { useStore } from 'vuex';
+
export default defineComponent({
name: 'PageLayout',
components: { FrameLayout },
setup() {
- const { getCaches } = useCache(true);
const { getShowMultipleTab } = useMultipleTabSetting();
const { getOpenKeepAlive, getCanEmbedIFramePage } = useRootSetting();
@@ -51,6 +51,17 @@
const openCache = computed(() => unref(getOpenKeepAlive) && unref(getShowMultipleTab));
+ const { getters } = useStore();
+
+ const getCaches = computed((): string[] => {
+ if (!unref(getOpenKeepAlive)) {
+ return [];
+ }
+ // TODO The useStore is used here mainly to solve the problem of circular dependency hot update
+ const cacheTabs = getters['app-tab/getCachedTabsState'];
+ return cacheTabs;
+ });
+
return {
getTransitionName,
openCache,
@@ -58,7 +69,6 @@
getBasicTransition,
getCaches,
getCanEmbedIFramePage,
- getKey,
};
},
});
diff --git a/src/layouts/page/useCache.ts b/src/layouts/page/useCache.ts
deleted file mode 100644
index 8cf0b796..00000000
--- a/src/layouts/page/useCache.ts
+++ /dev/null
@@ -1,63 +0,0 @@
-import type { FunctionalComponent } from 'vue';
-import type { RouteLocation } from 'vue-router';
-import { computed, ref, unref, getCurrentInstance } from 'vue';
-import { useRootSetting } from '/@/hooks/setting/useRootSetting';
-
-import { useRouter } from 'vue-router';
-import { useStore } from 'vuex';
-
-const ParentLayoutName = 'ParentLayout';
-
-const PAGE_LAYOUT_KEY = '__PAGE_LAYOUT__';
-
-export function getKey(component: FunctionalComponent & { type: Indexable }, route: RouteLocation) {
- return !!component?.type.parentView ? {} : { key: route.fullPath };
-}
-
-export function useCache(isPage: boolean) {
- const { getters } = useStore();
-
- const name = ref('');
- const { currentRoute } = useRouter();
- const instance = getCurrentInstance();
- const routeName = instance?.type.name;
- if (routeName && ![ParentLayoutName].includes(routeName)) {
- name.value = routeName;
- } else {
- const matched = currentRoute.value?.matched;
- if (!matched) {
- return;
- }
- const len = matched.length;
- if (len < 2) return;
- name.value = matched[len - 2].name as string;
- }
-
- const { getOpenKeepAlive } = useRootSetting();
-
- const getCaches = computed((): string[] => {
- if (!unref(getOpenKeepAlive)) {
- return [];
- }
- const cached = getters['app-tab/getCachedMapState'];
-
- if (isPage) {
- // page Layout
- return cached.get(PAGE_LAYOUT_KEY) || [];
- }
- const cacheSet = new Set();
- cacheSet.add(unref(name));
-
- const list = cached.get(unref(name));
-
- if (!list) {
- return Array.from(cacheSet);
- }
- list.forEach((item) => {
- cacheSet.add(item);
- });
-
- return Array.from(cacheSet);
- });
- return { getCaches };
-}
diff --git a/src/logics/mitt/tabChange.ts b/src/logics/mitt/tabChange.ts
index 18692f5c..90c0522b 100644
--- a/src/logics/mitt/tabChange.ts
+++ b/src/logics/mitt/tabChange.ts
@@ -4,7 +4,7 @@
import Mitt from '/@/utils/mitt';
import type { RouteLocationNormalized } from 'vue-router';
-import { getRoute } from '/@/router/helper/routeHelper';
+import { getRawRoute } from '/@/utils';
const mitt = new Mitt();
@@ -13,7 +13,7 @@ const key = Symbol();
let lastChangeTab: RouteLocationNormalized;
export function setLastChangeTab(lastChangeRoute: RouteLocationNormalized) {
- const r = getRoute(lastChangeRoute);
+ const r = getRawRoute(lastChangeRoute);
mitt.emit(key, r);
lastChangeTab = r;
}
diff --git a/src/router/constant.ts b/src/router/constant.ts
index 9f2f9513..3f7104e1 100644
--- a/src/router/constant.ts
+++ b/src/router/constant.ts
@@ -1,9 +1,7 @@
-import type { AppRouteRecordRaw } from '/@/router/types';
-import ParentLayout from '/@/layouts/page/ParentView.vue';
-import { t } from '/@/hooks/web/useI18n';
-
export const REDIRECT_NAME = 'Redirect';
+export const PARENT_LAYOUT_NAME = 'ParentLayout';
+
export const EXCEPTION_COMPONENT = () => import('../views/sys/exception/Exception.vue');
/**
@@ -12,78 +10,23 @@ export const EXCEPTION_COMPONENT = () => import('../views/sys/exception/Exceptio
export const LAYOUT = () => import('/@/layouts/default/index.vue');
/**
- * @description: page-layout
+ * @description: parent-layout
*/
-export const getParentLayout = (name: string) => {
+export const getParentLayout = (_name?: string) => {
return () =>
new Promise((resolve) => {
resolve({
- ...ParentLayout,
- name,
+ name: PARENT_LAYOUT_NAME,
});
});
};
-// 404 on a page
-export const PAGE_NOT_FOUND_ROUTE: AppRouteRecordRaw = {
- path: '/:path(.*)*',
- name: 'ErrorPage',
- component: LAYOUT,
- meta: {
- title: 'ErrorPage',
- hideBreadcrumb: true,
- },
- children: [
- {
- path: '/:path(.*)*',
- name: 'ErrorPage',
- component: EXCEPTION_COMPONENT,
- meta: {
- title: 'ErrorPage',
- hideBreadcrumb: true,
- },
- },
- ],
-};
-
-export const REDIRECT_ROUTE: AppRouteRecordRaw = {
- path: '/redirect',
- name: REDIRECT_NAME,
- component: LAYOUT,
- meta: {
- title: REDIRECT_NAME,
- hideBreadcrumb: true,
- },
- children: [
- {
- path: '/redirect/:path(.*)',
- name: REDIRECT_NAME,
- component: () => import('/@/views/sys/redirect/index.vue'),
- meta: {
- title: REDIRECT_NAME,
- hideBreadcrumb: true,
- },
- },
- ],
-};
-
-export const ERROR_LOG_ROUTE: AppRouteRecordRaw = {
- path: '/error-log',
- name: 'errorLog',
- component: LAYOUT,
- meta: {
- title: 'ErrorLog',
- hideBreadcrumb: true,
- },
- children: [
- {
- path: 'list',
- name: 'errorLogList',
- component: () => import('/@/views/sys/error-log/index.vue'),
- meta: {
- title: t('routes.basic.errorLogList'),
- hideBreadcrumb: true,
- },
- },
- ],
-};
+// export const getParentLayout = (name: string) => {
+// return () =>
+// new Promise((resolve) => {
+// resolve({
+// ...ParentLayout,
+// name,
+// });
+// });
+// };
diff --git a/src/router/guard/permissionGuard.ts b/src/router/guard/permissionGuard.ts
index ac20041b..5bd7d19d 100644
--- a/src/router/guard/permissionGuard.ts
+++ b/src/router/guard/permissionGuard.ts
@@ -5,7 +5,7 @@ import { permissionStore } from '/@/store/modules/permission';
import { PageEnum } from '/@/enums/pageEnum';
import { userStore } from '/@/store/modules/user';
-import { PAGE_NOT_FOUND_ROUTE } from '/@/router/constant';
+import { PAGE_NOT_FOUND_ROUTE } from '/@/router/routes/basic';
const LOGIN_PATH = PageEnum.BASE_LOGIN;
diff --git a/src/router/helper/menuHelper.ts b/src/router/helper/menuHelper.ts
index da48abcd..acb7dde0 100644
--- a/src/router/helper/menuHelper.ts
+++ b/src/router/helper/menuHelper.ts
@@ -1,44 +1,34 @@
import { AppRouteModule } from '/@/router/types';
import type { MenuModule, Menu, AppRouteRecordRaw } from '/@/router/types';
-import { findPath, forEach, treeMap } from '/@/utils/helper/treeHelper';
+import { findPath, treeMap } from '/@/utils/helper/treeHelper';
import { cloneDeep } from 'lodash-es';
import { isUrl } from '/@/utils/is';
-export function getAllParentPath(treeData: any[], path: string) {
+export function getAllParentPath(treeData: T[], path: string) {
const menuList = findPath(treeData, (n) => n.path === path) as Menu[];
return (menuList || []).map((item) => item.path);
}
-// 拼接父级路径
-function joinParentPath(list: any, node: any) {
- let allPaths = getAllParentPath(list, node.path);
-
- allPaths = allPaths.slice(0, allPaths.length - 1);
- let parentPath = '';
- if (Array.isArray(allPaths) && allPaths.length >= 2) {
- parentPath = allPaths[allPaths.length - 1];
- } else {
- allPaths.forEach((p) => {
- parentPath += /^\//.test(p) ? p : `/${p}`;
- });
+function joinParentPath(menus: Menu[], parentPath = '') {
+ for (let index = 0; index < menus.length; index++) {
+ const menu = menus[index];
+ const p = menu.path.startsWith('/') ? menu.path : `/${menu.path}`;
+ const parent = isUrl(menu.path) ? menu.path : `${parentPath}${p}`;
+ menus[index].path = parent;
+ if (menu?.children?.length) {
+ joinParentPath(menu.children, parent);
+ }
}
- node.path = `${/^\//.test(node.path) ? node.path : `${parentPath}/${node.path}`}`.replace(
- /\/\//g,
- '/'
- );
- return node;
}
-// 解析菜单模块
+// Parsing the menu module
export function transformMenuModule(menuModule: MenuModule): Menu {
const { menu } = menuModule;
const menuList = [menu];
- forEach(menuList, (m) => {
- !isUrl(m.path) && joinParentPath(menuList, m);
- });
+ joinParentPath(menuList);
return menuList[0];
}
@@ -54,17 +44,16 @@ export function transformRouteToMenu(routeModList: AppRouteModule[]) {
routeList.push(item);
}
});
- return treeMap(routeList, {
+ const list = treeMap(routeList, {
conversion: (node: AppRouteRecordRaw) => {
- const { meta: { title, icon, hideMenu = false } = {} } = node;
-
- !isUrl(node.path) && joinParentPath(routeList, node);
+ const { meta: { title, hideMenu = false } = {} } = node;
return {
+ ...(node.meta || {}),
name: title,
- icon,
- path: node.path,
hideMenu,
};
},
});
+ joinParentPath(list);
+ return list;
}
diff --git a/src/router/helper/routeHelper.ts b/src/router/helper/routeHelper.ts
index 03adedd8..10276175 100644
--- a/src/router/helper/routeHelper.ts
+++ b/src/router/helper/routeHelper.ts
@@ -1,22 +1,18 @@
import type { AppRouteModule, AppRouteRecordRaw } from '/@/router/types';
-import type { RouteLocationNormalized, RouteRecordNormalized } from 'vue-router';
+import type { Router, RouteRecordNormalized } from 'vue-router';
import { getParentLayout, LAYOUT } from '/@/router/constant';
import { cloneDeep } from 'lodash-es';
import { warn } from '/@/utils/log';
+import { createRouter, createWebHashHistory } from 'vue-router';
export type LayoutMapKey = 'LAYOUT';
const LayoutMap = new Map Promise>();
-let dynamicViewsModules: Record<
- string,
- () => Promise<{
- [key: string]: any;
- }>
->;
+let dynamicViewsModules: Record Promise>;
-// 动态引入
+// Dynamic introduction
function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
dynamicViewsModules = dynamicViewsModules || import.meta.glob('../../views/**/*.{vue,tsx}');
if (!routes) return;
@@ -26,19 +22,14 @@ function asyncImportRoute(routes: AppRouteRecordRaw[] | undefined) {
if (component) {
item.component = dynamicImport(dynamicViewsModules, component as string);
} else if (name) {
- item.component = getParentLayout(name);
+ item.component = getParentLayout();
}
children && asyncImportRoute(children);
});
}
function dynamicImport(
- dynamicViewsModules: Record<
- string,
- () => Promise<{
- [key: string]: any;
- }>
- >,
+ dynamicViewsModules: Record Promise>,
component: string
) {
const keys = Object.keys(dynamicViewsModules);
@@ -84,18 +75,69 @@ export function transformObjToRoute(routeList: AppRouteModul
return (routeList as unknown) as T[];
}
-// Return to the new routing structure, not affected by the original example
-export function getRoute(route: RouteLocationNormalized): RouteLocationNormalized {
- if (!route) return route;
- const { matched, ...opt } = route;
- return {
- ...opt,
- matched: (matched
- ? matched.map((item) => ({
- meta: item.meta,
- name: item.name,
- path: item.path,
- }))
- : undefined) as RouteRecordNormalized[],
- };
+/**
+ * Convert multi-level routing to level 2 routing
+ */
+export function flatRoutes(routeModules: AppRouteModule[]) {
+ for (let index = 0; index < routeModules.length; index++) {
+ const routeModule = routeModules[index];
+ if (!isMultipleRoute(routeModule)) {
+ continue;
+ }
+ promoteRouteLevel(routeModule);
+ }
+}
+
+// Routing level upgrade
+function promoteRouteLevel(routeModule: AppRouteModule) {
+ // Use vue-router to splice menus
+ let router: Router | null = createRouter({
+ routes: [routeModule as any],
+ history: createWebHashHistory(),
+ });
+
+ const routes = router.getRoutes();
+ const children = cloneDeep(routeModule.children);
+ addToChildren(routes, children || [], routeModule);
+ router = null;
+
+ routeModule.children = routeModule.children?.filter((item) => !item.children?.length);
+}
+
+// Add all sub-routes to the secondary route
+function addToChildren(
+ routes: RouteRecordNormalized[],
+ children: AppRouteRecordRaw[],
+ routeModule: AppRouteModule
+) {
+ for (let index = 0; index < children.length; index++) {
+ const child = children[index];
+ const route = routes.find((item) => item.name === child.name);
+ if (route) {
+ routeModule.children = routeModule.children || [];
+ routeModule.children?.push(route as any);
+ if (child.children?.length) {
+ addToChildren(routes, child.children, routeModule);
+ }
+ }
+ }
+}
+
+// Determine whether the level exceeds 2 levels
+function isMultipleRoute(routeModule: AppRouteModule) {
+ if (!routeModule || !Reflect.has(routeModule, 'children') || !routeModule.children?.length) {
+ return false;
+ }
+
+ const children = routeModule.children;
+
+ let flag = false;
+ for (let index = 0; index < children.length; index++) {
+ const child = children[index];
+ if (child.children?.length) {
+ flag = true;
+ break;
+ }
+ }
+ return flag;
}
diff --git a/src/router/menus/index.ts b/src/router/menus/index.ts
index ef3deb00..1c54d23e 100644
--- a/src/router/menus/index.ts
+++ b/src/router/menus/index.ts
@@ -5,6 +5,7 @@ import { appStore } from '/@/store/modules/app';
import { permissionStore } from '/@/store/modules/permission';
import { transformMenuModule, getAllParentPath } from '/@/router/helper/menuHelper';
import { filter } from '/@/utils/helper/treeHelper';
+import { isUrl } from '/@/utils/is';
import router from '/@/router';
import { PermissionModeEnum } from '/@/enums/appEnum';
import { pathToRegexp } from 'path-to-regexp';
@@ -19,8 +20,6 @@ Object.keys(modules).forEach((key) => {
menuModules.push(...modList);
});
-const reg = /(((https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;
-
// ===========================
// ==========Helper===========
// ===========================
@@ -40,18 +39,15 @@ const staticMenus: Menu[] = [];
})();
async function getAsyncMenus() {
- // 前端角色控制菜单 直接取菜单文件
return !isBackMode() ? staticMenus : permissionStore.getBackMenuListState;
}
-// 获取菜单 树级
export const getMenus = async (): Promise