@@ -6,52 +6,72 @@ let asyncRouterFlag = 0
6
6
const whiteList = [ 'Login' , 'Init' ]
7
7
8
8
const getRouter = async ( ) => {
9
- await store . dispatch ( 'router/SetAsyncRouter' )
10
- await store . dispatch ( 'user/GetUserInfo' )
11
- const asyncRouters = store . getters [ 'router/asyncRouters' ]
12
- asyncRouters . forEach ( asyncRouter => {
13
- router . addRoute ( asyncRouter )
14
- } )
9
+ await store . dispatch ( 'router/SetAsyncRouter' )
10
+ await store . dispatch ( 'user/GetUserInfo' )
11
+ const asyncRouters = store . getters [ 'router/asyncRouters' ]
12
+ asyncRouters . forEach ( asyncRouter => {
13
+ router . addRoute ( asyncRouter )
14
+ } )
15
15
}
16
16
17
- router . beforeEach ( async ( to , from , next ) => {
18
- const token = store . getters [ 'user/token' ]
19
- // 在白名单中的判断情况
20
- document . title = getPageTitle ( to . meta . title )
21
- if ( whiteList . indexOf ( to . name ) > - 1 ) {
22
- if ( token ) {
23
- if ( ! asyncRouterFlag ) {
24
- asyncRouterFlag ++
25
- await getRouter ( )
26
- }
27
- next ( { name : store . getters [ 'user/userInfo' ] . authority . defaultRouter } )
28
- } else {
29
- next ( )
17
+ async function handleKeepAlive ( to ) {
18
+ if ( to . matched && to . matched . length > 2 ) {
19
+ for ( let i = 1 ; i < to . matched . length ; i ++ ) {
20
+ const element = to . matched [ i - 1 ]
21
+ if ( element . name === "layout" ) {
22
+ to . matched . splice ( i , 1 )
23
+ await handleKeepAlive ( to )
24
+ }
25
+ // 如果没有按需加载完成则等待加载
26
+ if ( typeof element . components . default === 'function' ) {
27
+ await element . components . default ( )
28
+ await handleKeepAlive ( to )
29
+ }
30
+ }
30
31
}
31
- } else {
32
- // 不在白名单中并且已经登陆的时候
33
- if ( token ) {
34
- // 添加flag防止多次获取动态路由和栈溢出
35
- if ( ! asyncRouterFlag ) {
36
- asyncRouterFlag ++
37
- await getRouter ( )
38
- next ( { ...to , replace : true } )
39
- } else {
40
- if ( to . matched . length ) {
41
- next ( )
32
+ }
33
+
34
+ router . beforeEach ( async ( to , from , next ) => {
35
+ to . meta . matcheds = JSON . parse ( JSON . stringify ( to . matched ) )
36
+ handleKeepAlive ( to )
37
+ const token = store . getters [ 'user/token' ]
38
+ // 在白名单中的判断情况
39
+ document . title = getPageTitle ( to . meta . title )
40
+ if ( whiteList . indexOf ( to . name ) > - 1 ) {
41
+ if ( token ) {
42
+ if ( ! asyncRouterFlag ) {
43
+ asyncRouterFlag ++
44
+ await getRouter ( )
45
+ }
46
+ next ( { name : store . getters [ 'user/userInfo' ] . authority . defaultRouter } )
42
47
} else {
43
- next ( { path : '/layout/404' } )
48
+ next ( )
44
49
}
45
- }
46
- }
47
- // 不在白名单中并且未登陆的时候
48
- if ( ! token ) {
49
- next ( {
50
- name : 'Login' ,
51
- query : {
52
- redirect : document . location . hash
50
+ } else {
51
+ // 不在白名单中并且已经登陆的时候
52
+ if ( token ) {
53
+ // 添加flag防止多次获取动态路由和栈溢出
54
+ if ( ! asyncRouterFlag ) {
55
+ asyncRouterFlag ++
56
+ await getRouter ( )
57
+
58
+ next ( { ...to , replace : true } )
59
+ } else {
60
+ if ( to . matched . length ) {
61
+ next ( )
62
+ } else {
63
+ next ( { path : '/layout/404' } )
64
+ }
65
+ }
66
+ }
67
+ // 不在白名单中并且未登陆的时候
68
+ if ( ! token ) {
69
+ next ( {
70
+ name : 'Login' ,
71
+ query : {
72
+ redirect : document . location . hash
73
+ }
74
+ } )
53
75
}
54
- } )
55
76
}
56
- }
57
- } )
77
+ } )
0 commit comments