0
点赞
收藏
分享

微信扫一扫

Vue路由拦截

zhoulujun 2022-04-19 阅读 54
vue.js

apis.js 验证token

export var checktoken=(token)=>axios.get("/users/checktoken",{params:{token}})

index.js  路由拦截

// 切换路由之前 导航守卫  路由拦截
router.beforeEach((to, from, next) => {
  //只要不是默认登录 都开启路由验证
  if (to.path != '/' ) {
    //验证用户是否登录 token
    checktoken(localStorage.token).then(
      res=>{
        // console.log(res)
        if(res.data.code==0){
          next()  //正常跳转
        }
        else
        next('/')
      }
    )
  }else{
    next()
  }
  // to 要跳转的路由 from 来自于哪里  next 下一步
  // 如果用户未能验证身份,则 `next` 会被调用两次
  // next() //执行下一步
  // console.log(to)
  // console.log(from)

})
举报

相关推荐

0 条评论