在写代码的时候,用到router.beforeEach时,直接使用next跳转,结果死循环了:
const result = await cookies.getCookie('token')
LoadingBar.start()
if (!result) {
next('/BackLogin')
LoadingBar.stop()
}
原因就在于next在进入BackLogin前会再一次的执行beforeEach,这样就会一直循环下去,导致死循环,所以解决方法就是直接跳出死循环就好了。
if (!result) {
if (to.path === '/BackLogin') { // 防止死循环
next()
} else {
next('/BackLogin')
}
} else {
next()
}
解决!