vue导航前置守卫

堆栈溢出不知道为什么

routers.beforeEach(async (to, from, next) => {
    console.log('to',to)
    console.log('from',from)
    console.log('next',next)
    var userinfo = store.state.user.userinfo
    console.log(userinfo.name)
    // ----------------
//     if (to.path === '/login') next()
//   // 增加判断条件
//   if (!userinfo.name && to.path !== '/login') return next('/login')
//   next()
//   -----------------------
    if(userinfo.name !== undefined){
        // const toPath = to.path
        // if(toPath.indexOf('/login') !== -1|| toPath.indexOf('/register') !== -1){
        //     alert('请退出登录')
        //     next('/page')
        // }else{
        //     next()
        // }
        next()
    }  
    else if(!userinfo.name && to.path !== '/login'){
        next('/login')
        next()
    }else{
        next()
    }
})
if(userinfo.name){
        next()
    }else{
        const toPath = to.path
        if(toPath.indexOf('/page') !== -1){
            next('/login?redirect=' + toPath)
        }else{
            next()
        }
    }

你可能感兴趣的:(vue)