Vue-router路由判断页面是否登录,未登录跳转到登录页面

在index.js中

//定义路由
const router = new Router({
    routes,
    strict: process.env.NODE_ENV !== 'production',
})
//拦截器
router.beforeEach((route, redirect, next) => {if (!sessionStorage.getItem("userid") && route.path !== '/') {
        next({
            path: '/',
            query: {redirect: route.fullPath}
        })
    } else {
        console.log("-------------------",route,redirect);
        next()
    }
})
export default router

在login.vue中登录成功中加入

if(this.$route.query.redirect){
  let redirect = this.$route.query.redirect;
   this.$router.push(redirect);
}else{
  //默认登录成功后进入的页面
  this.$router.push('manage');
}                    

猜你喜欢

转载自www.cnblogs.com/zhoushuang0426/p/11125960.html
今日推荐