1
2
3
4
import type { Router } from 'vue-router';
import { tabStore } from '/@/store/modules/tab';
import { appStore } from '/@/store/modules/app';
import { userStore } from '/@/store/modules/user';
vben
authored
5 years ago
5
import { getParams } from '/@/utils/helper/routeHelper';
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
export function createPageLoadingGuard(router: Router) {
let isFirstLoad = true;
router.beforeEach(async (to) => {
const {
openKeepAlive,
openRouterTransition,
openPageLoading,
multiTabsSetting: { show } = {},
} = appStore.getProjectConfig;
if (!userStore.getTokenState) {
return true;
}
if (!openRouterTransition && openPageLoading) {
appStore.commitPageLoadingState(true);
return true;
}
if (show && openKeepAlive && !isFirstLoad) {
const tabList = tabStore.getTabsState;
vben
authored
5 years ago
25
26
27
28
29
30
31
32
const isOpen = tabList.some((tab) => tab.path === to.path);
appStore.setPageLoadingAction(!isOpen);
} else {
appStore.setPageLoadingAction(true);
}
return true;
});
vben
authored
5 years ago
33
router.afterEach(async (to, from) => {
34
const { openRouterTransition, openPageLoading } = appStore.getProjectConfig;
vben
authored
5 years ago
35
36
37
38
39
40
41
42
const realToPath = to.path.replace(getParams(to), '');
const realFormPath = from.path.replace(getParams(from), '');
if (
(!openRouterTransition && openPageLoading) ||
isFirstLoad ||
to.meta.afterCloseLoading ||
realToPath === realFormPath
) {
43
44
45
setTimeout(() => {
appStore.commitPageLoadingState(false);
}, 110);
46
47
isFirstLoad = false;
}
48
49
50
51
return true;
});
}