Vue实现记住用户状态时的页面跳转

在我们登陆完成时,我们不希望进入某些页面时是登录前的状态,或者有跳转方式的改变,那么这时候既可以使用导航守卫进行路由拦截。

官方文档

这里加上路由元信息,遍历组件针对性的管理页面。

router.beforeEach((to, from, next) => {
    
    
    if (to.matched.some(record => record.meta.requireAuth)) {
    
    
        if (localStorage.getItem('token')) {
    
    
            if (to.name !== 'Login') next()
            else next({
    
    
                name: "Userhome"
            })
        } else {
    
    
            if (to.name === 'Login') {
    
             //防止循环调用
                next()
            } else {
    
    
                next({
    
    
                    path: 'Login'
                })
            }
        }
    }
    next()
})

Guess you like

Origin blog.csdn.net/weixin_45509601/article/details/120243283