vue路由设置,登录失效后跳转到去登录页面。登录后打开的是你最后登录的页面

main.js

router.beforeEach((to, from, next) => {
    document.title=to.name;   //让页面title显示路由对应的name值--xlz
  if (to.path == '/login') {
    sessionStorage.removeItem('user');
  }
  let user = JSON.parse(sessionStorage.getItem('user')); 
  if (!user && to.path != '/login') {   //如果获取不到user说明登录失效了
      if( to.path == '/regist/regist' || to.path == '/mobile/exampaper'){
        next();
      }else{
        next({ 
            path: '/login',
            query: {redirect: to.fullPath} // 将跳转的路由path作为参数,登录成功后跳转到该路由-xlz
        });
      }
  } else {
    next()
  }
})

然后再login页面写

 在登录的时候把user 也就是用户的信息存起来。sessionStorage.setItem('user', JSON.stringify(data));

登陆成功跳转页面时不能写死写成首页,而是下面这个:

let redirect = decodeURIComponent(this.$route.query.redirect || '/main'); 
this.$router.push({ path: redirect })// 获取登录成功后要跳转的路由。decodeURIComponent函数编码的 URI 进行解码--xlz

猜你喜欢

转载自blog.csdn.net/qq_33769914/article/details/83022151
今日推荐