vue路由拦截器遇到的坑

思路,所有的跳转该域名的url,都会跳转到登陆login页面,在login页面输入正确的账号和密码后,后台返回一个token,存在sessionStorage 里面,拦截器读取到token就放

router.beforeEach((to,from,next) => {
  let Pcurl = router.options.routes[1].children;
  var se = window.sessionStorage;
  if(!se.single){
    Pcurl.some( item =>{
      if(to.path == `/${item.path}`){
        console.log(from.path)
        return next('login')
      }
    })
  }
  next()
})

有意思的出现了,正常请求域名全部跳转到login界面,一切正常,但是在login界面区请求其他页面的时候,居然跳转了,拦截器没有生效。

总结:

从next中的页面再拦截进入同界面,拦截器会失效,next(''login)中是拦截到login界面,但是你从login界面出发到其他页面next('login')就会失效,总而言之这玩意不能刷新

解决办法

在判定跳转页面前判定一次from是否为login,如果是就直接返回next反正是绝对不能用

router.beforeEach((to,from,next) => {
  let Pcurl = router.options.routes[1].children;
  var se = window.sessionStorage;
  if(!se.single){
    if(from.path == '/login'){
      return false
    }
    Pcurl.some( item =>{
      if(to.path == `/${item.path}`){
        console.log(from.path)
        return next('login')
      }
    })
  }
  next()
})

 

你可能感兴趣的:(vue)