在使用VueRouter的全局前置守卫进行登录鉴权操作时,路由报错'Detected an infinite redirection in a navigation guard when going from "/" to "/". Aborting to avoid a Stack Overflow. This will break in production if not fixed.'
检查后发现 :在使用 router.beforEach时,内部判断导致路由产生了死循环...
代码如下 :
router.beforeEach((to, from, next) => {
if (store.getters.token) {
if (to.path === '/login') {
next('/')
} else {
next()
}
} else {
next('/login')
}
})
报错:
解决 :
在每个判断语句中都需要有next()进行处理
router.beforeEach((to, from, next) => {
if (store.getters.token) {
if (to.path === '/login') {
next('/')
} else {
next()
}
} else {
if (whiteList.indexOf(to.path) > -1) {
next()
} else {
next('/login')
}
}
})