首页 > 其他 > 详细

vue-router beforeEach出现死循环

时间:2021-05-17 21:51:19      阅读:16      评论:0      收藏:0      [点我收藏+]

vue router 在beforeEach处理登录出现问题

有问题代码如下:

router.beforeEach((to, from, next) => {
  
  let token = localStorage.getItem(‘token‘)
  if(token){
    next()
  }else{
    if(to.fullPath == ‘/‘){
      next()
    } else{
      next({ path: ‘/login‘ })
      //next()
    }
  }

})

  

当未获取到token时候,需要进入到login页面,此时会一直出现死循环,报错如下

技术分享图片

 

 

 原因:因为进入login页面时候,/login又会进入beforeEach里面,这样就造成了死循环

  

  解决方式如下:

let token = localStorage.getItem(‘token‘)
  if(token){
    next()
  }else{
    if(to.fullPath == ‘/‘){
      next()
    } else if (to.fullPath == ‘/login‘){
      next()
    }else{
      next({ path: ‘/login‘ })
      //next()
    }
  }

  

vue-router beforeEach出现死循环

原文:https://www.cnblogs.com/willsoo/p/14777977.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!