关于vue的router使用beforeEach造成死循环的问题

一般你会这样写

router.beforeEach((to, from, next) => {
    const isLogin = sessionStorage.getItem('loginData')
    if (isLogin) {
        next()
    } else {
        next('/error')
    } 
})
View Code

这样会造成死循环,解决办法

router.beforeEach((to, from, next) => {
    const isLogin = sessionStorage.getItem('loginData')
    if (isLogin) {
        next()
    } else {
        if (to.path === '/login') { //这就是跳出循环的关键
           next()
        } else {
            next('/login')
        }
    } 
})
View Code
zhumiao
原文地址:https://www.cnblogs.com/zhumiao/p/9543791.html