Vue登录界面登录成功无法跳转

 
 

     前言:

           在试着用vue写一个登录页面,已经调试好了,登录成功后,跳转到主页面。但是加了拦截器后,不会跳到主页面

 this.$axios.post(domain.publicUrl+'/login',
                    this.$qs.stringify(data1)).then(res=>{
                      if(res.data.code===200){             
                          this.$router.replace('/main'),
              window.localStorage["token"]=JSON.stringify(res.data.data.token);

                      }else {
                          this.$message({
                              type:"info",
                              message: res.data.msg
                          });
                      }
                });

       二、在这种情况下登录功能,跳转都正常,后来加入了拦截器,代码如下:

router.beforeEach((to, from, next) => {
  if (to.meta.requireAuth) {  // 判断该路由是否需要登录权限
    if (localStorage.token) {  // 获取当前的token是否存在
      console.log("token存在");
      next();
    } else {
      console.log("token不存在");
      next({
        path: '/login', // 将跳转的路由path作为参数,登录成功后跳转到该路由
        query: {redirect: to.fullPath}
      })
      return;
    }
  }

          加入拦截器后,拦截功能正常,跳转功能不正常了,一直都在登录界面,登录成功,token也写入缓存了,但是就是不会跳转,看拦截器代码也没问题,后来看登录的代码才发现了问题所在。我的跳转的代码是在写token之前的。所以跳转到main页面的时候,没有token就又返回到login页面,然后一直循环了。查了半天。哈哈哈 。。。  十一假期要结束了呀,没出去玩,快点上班吧。

posted @ 2020-10-06 22:34  大乗  阅读(7971)  评论(0编辑  收藏  举报