vue3+vite3(三)动态路由刷新路由失效问题

9 篇文章 0 订阅

问题:

根据用户角色动态生成路由表,如果不刷新页面,一切操作正常。但是如果直接在浏览器地址栏更改网址,或者刷新页面,路由会失效,并自动定位到404页面。

原因:

页面刷新时,路由重新初始化,动态添加的路由此时已不存在,只有一些固定路由(比如登录页面)还在,所以出现了404的情况

解决一:

第一步:

在路由的全局导航router.beforeEach处做个判断,根据动态路由表(resRoute)的长度来判断页面是否是刷新。如果不为0,则表示第一次登陆,生成了动态路由,登录后会走匹配路由的方法,不会有问题;如果length为0,表示刷新页面,需要重新执行路由匹配,重新添加动态路由即可。

第二步:

不要把404页面定义在固定路由里,而是在动态路由挂载后,再router.push()404页面。这样刷新页面就不会再出现404跳转了。

var resRoute = []//根据用户角色生成的路由表,直接在router中定义,没有使用vuex
router.beforeEach((to, from, next) => {
  NProgress.start()
  document.title = `${to.meta.title}`
  const token = localStorage.getItem("oo")
  let roles = ["common"]
  if (!token && to.path !== '/login') {
    next('/login')
  } else {
    if (resRoute.length !== 0) {//判断动态路由表的长度是否为0
      next()
    } else {
      resRoute = handelRoutes(permissionRoutes[0].children, roles)
      resRoute.forEach(route => {
        router.addRoute(route)
      })
      router.addRoute({//向动态路由添加404页面
        path: '/404',
        name: '404',
        component: () => import( /* webpackChunkName: "404" */ '../views/404.vue'),
        meta: {
          title: '路径错误'
        }
      })
      router.addRoute({
        // path: "*", // 这样用,vue3已经不支持,得下面的方式
        path: "/:pathMath(.*)", // 此处需特别注意置于最底部
        redirect: "/404"
      })
      // 如果 addRoute 并未完成,路由守卫会一层一层的执行执行,直到 addRoute 完成,找到对应的路由
      next({
        ...to,
        replace: true
      })
    }
  }

但是这个方法有一个小问题,就是控制台会警告找不到页面路径,但目前没有什么其他影响。评论区有知道的同学,麻烦指正下。

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue3中,可以通过使用`createRouter`函数来创建路由实例,同时使用`createWebHashHistory`或`createWebHistory`函数来创建不同类型的路由模式。在动态添加路由的过程中,需要使用`router.addRoute`方法向路由器中添加新的路由配置项。 下面是一个示例代码,展示如何使用Vue3、Vite和Layout布局动态添加路由: ```javascript // 引入相关模块 import { createRouter, createWebHashHistory } from 'vue-router'; import Layout from '@/layout/index.vue'; // 创建路由实例 const router = createRouter({ history: createWebHashHistory(), routes: [ { path: '/', component: Layout, children: [] } ] }); // 添加动态路由 const newRoutes = [ { path: '/dashboard', component: () => import('@/views/dashboard/index.vue'), name: 'Dashboard', meta: { title: 'Dashboard', icon: 'dashboard' } }, { path: '/example', component: Layout, redirect: '/example/table', name: 'Example', meta: { title: 'Example', icon: 'example' }, children: [ { path: 'table', name: 'Table', component: () => import('@/views/table/index.vue'), meta: { title: 'Table', icon: 'table' } }, { path: 'tree', name: 'Tree', component: () => import('@/views/tree/index.vue'), meta: { title: 'Tree', icon: 'tree' } } ] } ]; newRoutes.forEach(route => { router.addRoute(route); }); export default router; ``` 在上面的示例代码中,我们首先创建了一个包含一个空子路由路由配置项。然后,我们定义了一个数组变量`newRoutes`,用于存放需要动态添加的路由配置项。最后,我们通过`forEach`循环遍历`newRoutes`数组,并通过`router.addRoute`方法将每个路由配置项添加到路由器中。 需要注意的是,`router.addRoute`方法只能在初始化路由时使用,无法在路由已经初始化之后使用。因此,我们需要在`createRouter`函数中初始化路由,并在路由创建完成之后再添加动态路由

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值