Skip to content

Commit aeabe8c

Browse files
author
piexlmax
committed
1 parent ccfdfc3 commit aeabe8c

File tree

2 files changed

+63
-43
lines changed

2 files changed

+63
-43
lines changed

web/src/permission.js

Lines changed: 62 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,52 +6,72 @@ let asyncRouterFlag = 0
66
const whiteList = ['Login', 'Init']
77

88
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+
})
1515
}
1616

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+
}
3031
}
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 })
4247
} else {
43-
next({ path: '/layout/404' })
48+
next()
4449
}
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+
})
5375
}
54-
})
5576
}
56-
}
57-
})
77+
})

web/src/view/layout/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export default {
152152
return this.$route.meta.title || '当前页面'
153153
},
154154
matched() {
155-
return this.$route.matched
155+
return this.$route.meta.matcheds
156156
}
157157
},
158158
created() {

0 commit comments

Comments
 (0)