• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

mandyGuan12

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

vue3使用路由守卫出现的问题合集

  1. 登录后停留在登录页面无法跳转 --24.9.3
    原代码:
function isRoute (to) {
  let res = router.getRoutes()
  let resFil = res.filter(item => item.path === to.path)
  return resFil.length > 0
}
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !store.state.token) {
    next({ name: 'login' })
  } else if (!isRoute(to)) {
    next({ name: '404' })
  } else next()
})

看上去没有逻辑问题, log一下看看:

  console.log('to.path'+to.path);
  console.log("from.path"+from.path);
  console.log('token'+store.state.token);

打印出来发现token为空值, 检查login部分的逻辑代码:

const handleLogin = async () => {
  const res = await proxy.$api.getMenu(loginForm)
  console.log(res)
  store.setMenuList(res.menuList)
  store.token = res.token
  store.dynamicRoute(router)
  router.push('/home')
}

可以发现存储token的代码有问题, 应为: store.state.token = res.token

posted on 2024-09-03 16:53  番茄仔Mandy  阅读(72)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3