统一登录校验 误删用户登陆菜单后不再白屏

This commit is contained in:
蒋吉兆
2021-05-09 19:26:08 +08:00
parent 18cf39529e
commit 8159aa1a88
4 changed files with 18 additions and 12 deletions

View File

@@ -4,11 +4,11 @@ import getPageTitle from '@/utils/page'
let asyncRouterFlag = 0
const whiteList = ['login','init']
router.beforeEach(async(to, from, next) => {
const whiteList = ['login', 'init']
router.beforeEach(async (to, from, next) => {
const token = store.getters['user/token']
// 在白名单中的判断情况
//修改网页标签名称
// 在白名单中的判断情况
//修改网页标签名称
document.title = getPageTitle(to.meta.title)
if (whiteList.indexOf(to.name) > -1) {
if (token) {
@@ -24,10 +24,16 @@ router.beforeEach(async(to, from, next) => {
asyncRouterFlag++
await store.dispatch('router/SetAsyncRouter')
const asyncRouters = store.getters['router/asyncRouters']
router.addRoutes(asyncRouters)
next({...to, replace: true })
asyncRouters.forEach(item => {
router.addRoute(item)
})
next({ ...to, replace: true })
} else {
next()
if (to.matched.length) {
next()
} else {
next({ path: "/layout/404" })
}
}
}
// 不在白名单中并且未登陆的时候