Skip to content

Commit 327d71b

Browse files
committedJun 27, 2021
perf(router): reduce the number of guard files
1 parent 941ad59 commit 327d71b

21 files changed

+154
-177
lines changed
 

‎.vscode/settings.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@
129129
"qrcode",
130130
"sider",
131131
"pinia",
132-
"sider"
132+
"sider",
133+
"nprogress"
133134
]
134135
}

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
"mockjs": "^1.1.0",
5151
"nprogress": "^0.2.0",
5252
"path-to-regexp": "^6.2.0",
53-
"pinia": "2.0.0-beta.3",
53+
"pinia": "^2.0.0-beta.3",
5454
"print-js": "^1.6.0",
5555
"qrcode": "^1.4.4",
5656
"sortablejs": "^1.13.0",

‎src/App.vue

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
import { defineComponent } from 'vue';
1111
import { ConfigProvider } from 'ant-design-vue';
1212
import { AppProvider } from '/@/components/Application';
13-
1413
import { useTitle } from '/@/hooks/web/useTitle';
1514
import { useLocale } from '/@/locales/useLocale';
1615

‎src/components/Drawer/src/BasicDrawer.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
watch(
138138
() => props.visible,
139139
(newVal, oldVal) => {
140-
if (newVal != oldVal) visibleRef.value = newVal;
140+
if (newVal !== oldVal) visibleRef.value = newVal;
141141
},
142142
{ deep: true }
143143
);

‎src/components/Menu/src/BasicMenu.vue

-7
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,18 @@
1919
</template>
2020
<script lang="ts">
2121
import type { MenuState } from './types';
22-
2322
import { computed, defineComponent, unref, reactive, watch, toRefs, ref } from 'vue';
2423
import { Menu } from 'ant-design-vue';
2524
import BasicSubMenuItem from './components/BasicSubMenuItem.vue';
26-
2725
import { MenuModeEnum, MenuTypeEnum } from '/@/enums/menuEnum';
28-
2926
import { useOpenKeys } from './useOpenKeys';
3027
import { RouteLocationNormalizedLoaded, useRouter } from 'vue-router';
31-
3228
import { isFunction } from '/@/utils/is';
33-
3429
import { basicProps } from './props';
3530
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
3631
import { REDIRECT_NAME } from '/@/router/constant';
3732
import { useDesign } from '/@/hooks/web/useDesign';
38-
3933
import { getCurrentParentPath } from '/@/router/menus';
40-
4134
import { listenerRouteChange } from '/@/logics/mitt/routeChange';
4235
import { getAllParentPath } from '/@/router/helper/menuHelper';
4336

‎src/components/registerGlobComp.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
// Need
66
Button as AntButton,
77
Input,
8+
Layout,
89
} from 'ant-design-vue';
910

1011
const compList = [AntButton.Group];
@@ -14,5 +15,5 @@ export function registerGlobComp(app: App) {
1415
app.component(comp.name || comp.displayName, comp);
1516
});
1617

17-
app.use(Input).use(Button);
18+
app.use(Input).use(Button).use(Layout);
1819
}

‎src/layouts/default/menu/useLayoutMenu.ts

-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
import type { Menu } from '/@/router/types';
22
import type { Ref } from 'vue';
3-
43
import { watch, unref, ref, computed } from 'vue';
54
import { useRouter } from 'vue-router';
6-
75
import { MenuSplitTyeEnum } from '/@/enums/menuEnum';
86
import { useThrottleFn } from '@vueuse/core';
97
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
10-
118
import { getChildrenMenus, getCurrentParentPath, getMenus, getShallowMenus } from '/@/router/menus';
129
import { usePermissionStore } from '/@/store/modules/permission';
1310
import { useAppInject } from '/@/hooks/web/useAppInject';

‎src/layouts/default/sider/MixSider.vue

-5
Original file line numberDiff line numberDiff line change
@@ -81,24 +81,19 @@
8181
import type { Menu } from '/@/router/types';
8282
import type { CSSProperties } from 'vue';
8383
import type { RouteLocationNormalized } from 'vue-router';
84-
8584
import { defineComponent, onMounted, ref, computed, unref } from 'vue';
86-
8785
import { ScrollContainer } from '/@/components/Container';
8886
import { SimpleMenuTag } from '/@/components/SimpleMenu';
8987
import { Icon } from '/@/components/Icon';
9088
import { AppLogo } from '/@/components/Application';
9189
import Trigger from '../trigger/HeaderTrigger.vue';
92-
9390
import { useMenuSetting } from '/@/hooks/setting/useMenuSetting';
9491
import { useDragLine } from './useLayoutSider';
9592
import { useGlobSetting } from '/@/hooks/setting';
9693
import { useDesign } from '/@/hooks/web/useDesign';
9794
import { useI18n } from '/@/hooks/web/useI18n';
9895
import { useGo } from '/@/hooks/web/usePage';
99-
10096
import { SIDE_BAR_SHOW_TIT_MINI_WIDTH, SIDE_BAR_MINI_WIDTH } from '/@/enums/appEnum';
101-
10297
import clickOutside from '/@/directives/clickOutside';
10398
import { getShallowMenus, getChildrenMenus, getCurrentParentPath } from '/@/router/menus';
10499
import { listenerRouteChange } from '/@/logics/mitt/routeChange';

‎src/main.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import '/@/design/index.less';
22
import '/@/design/tailwind.css';
3-
43
// Register icon sprite
54
import 'virtual:svg-icons-register';
6-
75
import App from './App.vue';
86
import { createApp } from 'vue';
97
import { initAppConfigStore } from '/@/logics/initAppConfig';
@@ -42,7 +40,7 @@ async function bootstrap() {
4240
setupRouter(app);
4341

4442
// router-guard
45-
setupRouterGuard();
43+
setupRouterGuard(router);
4644

4745
// Register global directive
4846
setupGlobDirectives(app);

‎src/router/guard/httpGuard.ts

-20
This file was deleted.

‎src/router/guard/index.ts

+133-9
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
1-
import { router } from '/@/router';
2-
3-
import { createProgressGuard } from './progressGuard';
1+
import type { Router, RouteLocationNormalized } from 'vue-router';
2+
import { useAppStoreWidthOut } from '/@/store/modules/app';
3+
import { useUserStoreWidthOut } from '/@/store/modules/user';
4+
import { useTransitionSetting } from '/@/hooks/setting/useTransitionSetting';
5+
import { AxiosCanceler } from '/@/utils/http/axios/axiosCancel';
6+
import { Modal, notification } from 'ant-design-vue';
7+
import { warn } from '/@/utils/log';
8+
import { unref } from 'vue';
9+
import { setRouteChange } from '/@/logics/mitt/routeChange';
410
import { createPermissionGuard } from './permissionGuard';
5-
import { createPageLoadingGuard } from './pageLoadingGuard';
6-
import { createMessageGuard } from './messageGuard';
7-
import { createScrollGuard } from './scrollGuard';
8-
import { createHttpGuard } from './httpGuard';
9-
import { createPageGuard } from './pageGuard';
1011
import { createStateGuard } from './stateGuard';
12+
import nProgress from 'nprogress';
13+
import projectSetting from '/@/settings/projectSetting';
1114

12-
export function setupRouterGuard() {
15+
// Don't change the order of creation
16+
export function setupRouterGuard(router: Router) {
1317
createPageGuard(router);
1418
createPageLoadingGuard(router);
1519
createHttpGuard(router);
@@ -19,3 +23,123 @@ export function setupRouterGuard() {
1923
createPermissionGuard(router);
2024
createStateGuard(router);
2125
}
26+
27+
/**
28+
* Hooks for handling page state
29+
*/
30+
function createPageGuard(router: Router) {
31+
const loadedPageMap = new Map<string, boolean>();
32+
33+
router.beforeEach(async (to) => {
34+
// The page has already been loaded, it will be faster to open it again, you don’t need to do loading and other processing
35+
to.meta.loaded = !!loadedPageMap.get(to.path);
36+
// Notify routing changes
37+
setRouteChange(to);
38+
39+
return true;
40+
});
41+
42+
router.afterEach((to) => {
43+
loadedPageMap.set(to.path, true);
44+
});
45+
}
46+
47+
// Used to handle page loading status
48+
function createPageLoadingGuard(router: Router) {
49+
const userStore = useUserStoreWidthOut();
50+
const appStore = useAppStoreWidthOut();
51+
const { getOpenPageLoading } = useTransitionSetting();
52+
router.beforeEach(async (to) => {
53+
if (!userStore.getToken) {
54+
return true;
55+
}
56+
if (to.meta.loaded) {
57+
return true;
58+
}
59+
60+
if (unref(getOpenPageLoading)) {
61+
appStore.setPageLoadingAction(true);
62+
return true;
63+
}
64+
65+
return true;
66+
});
67+
router.afterEach(async () => {
68+
if (unref(getOpenPageLoading)) {
69+
// TODO Looking for a better way
70+
// The timer simulates the loading time to prevent flashing too fast,
71+
setTimeout(() => {
72+
appStore.setPageLoading(false);
73+
}, 220);
74+
}
75+
return true;
76+
});
77+
}
78+
79+
/**
80+
* The interface used to close the current page to complete the request when the route is switched
81+
* @param router
82+
*/
83+
function createHttpGuard(router: Router) {
84+
const { removeAllHttpPending } = projectSetting;
85+
let axiosCanceler: Nullable<AxiosCanceler>;
86+
if (removeAllHttpPending) {
87+
axiosCanceler = new AxiosCanceler();
88+
}
89+
router.beforeEach(async () => {
90+
// Switching the route will delete the previous request
91+
axiosCanceler?.removeAllPending();
92+
return true;
93+
});
94+
}
95+
96+
// Routing switch back to the top
97+
function createScrollGuard(router: Router) {
98+
const isHash = (href: string) => {
99+
return /^#/.test(href);
100+
};
101+
102+
const body = document.body;
103+
104+
router.afterEach(async (to) => {
105+
// scroll top
106+
isHash((to as RouteLocationNormalized & { href: string })?.href) && body.scrollTo(0, 0);
107+
return true;
108+
});
109+
}
110+
111+
/**
112+
* Used to close the message instance when the route is switched
113+
* @param router
114+
*/
115+
export function createMessageGuard(router: Router) {
116+
const { closeMessageOnSwitch } = projectSetting;
117+
118+
router.beforeEach(async () => {
119+
try {
120+
if (closeMessageOnSwitch) {
121+
Modal.destroyAll();
122+
notification.destroy();
123+
}
124+
} catch (error) {
125+
warn('message guard error:' + error);
126+
}
127+
return true;
128+
});
129+
}
130+
131+
export function createProgressGuard(router: Router) {
132+
const { getOpenNProgress } = useTransitionSetting();
133+
router.beforeEach(async (to) => {
134+
if (to.meta.loaded) {
135+
return true;
136+
}
137+
unref(getOpenNProgress) && nProgress.start();
138+
return true;
139+
});
140+
141+
router.afterEach(async () => {
142+
unref(getOpenNProgress) && nProgress.done();
143+
return true;
144+
});
145+
}

‎src/router/guard/messageGuard.ts

-24
This file was deleted.

‎src/router/guard/pageGuard.ts

-18
This file was deleted.

‎src/router/guard/pageLoadingGuard.ts

-34
This file was deleted.

0 commit comments

Comments
 (0)