丹丹的小跟班 2024-09-26 10:50 采纳率: 64.4%
浏览 33

vue3 页面刷新空白

为什么刷新页面会空白?我做了持久化,routes里面的数据是一直存在的,但是好像刷新后虽然有routes数据,但是之前添加到router里面的动态路由也消失了?


router.beforeEach(async (to, form, next) => {
  const { setAndFormatMenu } = useUserStore()

  if (to.path === '/login') {
    return next()
  }
  const { routes } = storeToRefs(useUserStore())
  
  if (routes.value.length === 0) {
    const newRoutes = await setAndFormatMenu()
    router.addRoute({
      path: '/',
      component: () => import('@/components/Layout/index.vue'),
      children: newRoutes,
    })
    const firstPage = getFirstPage(routes.value)
    return next(firstPage)
  }
  next()
})
export default router
  • 写回答

1条回答 默认 最新

  • caperxi 2024-09-26 15:16
    关注

    不知道你的完整代码是怎样,但不推荐新手在路由守卫里面去做动态路由,因为有可能对于某些判断的把控不好,会死循环,可以尝试在应用的生命周期里面去添加,一样可以

    如有帮助,请采纳!!!

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 9月26日