Vue3中路由配置Catch all routes (“*“) must .....问题

Vue3中路由配置Catch all routes ("*") must ...问题

文章目录

  • [Vue3中路由配置Catch all routes ("*") must .....问题](#Vue3中路由配置Catch all routes ("*") must .....问题)
  • [1. 业务场景描述](#1. 业务场景描述)
    • [1. 加载并添加异步路由场景](#1. 加载并添加异步路由场景)
    • [2. vue2中加载并添加异步路由(OK)](#2. vue2中加载并添加异步路由(OK))
    • [3. 转vue3后不好使(Error)](#3. 转vue3后不好使(Error))
      • [1. 代码](#1. 代码)
      • [2. 错误](#2. 错误)
  • [2. 处理方式](#2. 处理方式)
    • [1. 修改前](#1. 修改前)
    • [2. 修改后](#2. 修改后)
    • [3. vue3中完整代码案例](#3. vue3中完整代码案例)

1. 业务场景描述

1. 加载并添加异步路由场景

从vue2项目转换为Vue3项目时,路由导航守卫中加载后端返回的动态路由,并配置未识别的路由自动跳转指定错误页面(如404页面)时,出现了ue-router.mjs:1321 Uncaught (in promise) Error: Catch all routes ("*") must now be defined using a param with a custom regexp 的问题

2. vue2中加载并添加异步路由(OK)

Vue2中路由导航守卫中加载动态路由案例代码如下

js 复制代码
let asyncRouter = []
// 路由导航守卫中,加载动态路由
router.beforeEach((to, from, next) => {
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    const token = tokenStore.get('token')
    if (token) {
      dbApi.getRouter({}).then((response) => {
        const res = response.data
        asyncRouter = res.data
        asyncRouter.push({       
          component: "error/404",
          name: "404",
          path: "*" //问题主要出现在这里
        });
        store.commit('setRouters', asyncRouter)
        goTo(to, next,asyncRouter)
      })
    } else {
      if (to.path === '/') {
        next()
      }
    }
  }
})

router.afterEach(() => {
  //....
})

function goTo(to, next,asyncRouter) {
  router.addRoutes(asyncRouter) //注意这里时Vue2中添加路由的方法,与Vue3有所区别
  next({...to, replace: true})
}

3. 转vue3后不好使(Error)

1. 代码

ts 复制代码
let asyncRouter = []
// 路由导航守卫中,加载动态路由
router.beforeEach((to, from, next) => {
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    const accountStore = useAccountStore();
    const token = accountStore.token
    if (token) {
      dbApi.getRouter({}).then((response) => {
        const res = response.data
        asyncRouter = res.data
        asyncRouter.push({       
          component: "error/404",
          name: "404",
          path: "*" //问题主要出现在这里
        });
        store.commit('setRouters', asyncRouter)
        goTo(to, next,asyncRouter)
      })
    } else {
      if (to.path === '/') {
        next()
      }
    }
  }
})

router.afterEach(() => {
  //....
})

function goTo(to, next,asyncRouter) {
    asyncRouter.forEach((route) => {     
        router.addRoute(route) //注意这里vue3添加路由方式,与Vue2有所区别
    })
     next({...to, replace: true})
}

2. 错误

详细信息如下

shell 复制代码
vue-router.mjs:1321  Uncaught (in promise) Error: Catch all routes ("*") must now be defined using a param with a custom regexp.
See more at https://next.router.vuejs.org/guide/migration/#removed-star-or-catch-all-routes.
    at Object.addRoute (vue-router.mjs:1321:23)
    at Object.addRoute (vue-router.mjs:2986:24)
    at index.ts:119:16
    at Array.forEach (<anonymous>)
    at go (index.ts:117:17)
    at index.ts:93:25

2. 处理方式

未识别的路由自动跳转指定错误页面(如404页面)时,将路由中的path配置{ path: "*"}改为{path: "/:catchAll(.*)"}即可

1. 修改前

js 复制代码
 asyncRouter.push({       
    component: "error/404",
    name: "404",
     path: "*"
 });

2. 修改后

js 复制代码
 asyncRouter.push({       
    component: "error/404",
    name: "404",
    path: "/:catchAll(.*)"
 });

3. vue3中完整代码案例

js 复制代码
let asyncRouter = []
// 路由导航守卫中,加载动态路由
router.beforeEach((to, from, next) => {
  if (whiteList.indexOf(to.path) !== -1) {
    next()
  } else {
    const accountStore = useAccountStore();
    const token = accountStore.token
    if (token) {
      dbApi.getRouter({}).then((response) => {
        const res = response.data
        asyncRouter = res.data
        asyncRouter.push({       
          component: "error/404",
          name: "404",
          path: "/:catchAll(.*)"
        });
        store.commit('setRouters', asyncRouter)
        goTo(to, next,asyncRouter)
      })
    } else {
      if (to.path === '/') {
        next()
      }
    }
  }
})

router.afterEach(() => {
  //....
})

function goTo(to, next,asyncRouter) {
    asyncRouter.forEach((route) => {     
        router.addRoute(route) //注意这里是vue3添加路由方式,与Vue2有所区别
    })
     next({...to, replace: true})
}
相关推荐
满怀101519 分钟前
【HTML 全栈进阶】从语义化到现代 Web 开发实战
前端·html
东锋1.330 分钟前
前端动画库 Anime.js 的V4 版本,兼容 Vue、React
前端·javascript·vue.js
满怀101543 分钟前
【Flask全栈开发指南】从零构建企业级Web应用
前端·python·flask·后端开发·全栈开发
小杨升级打怪中1 小时前
前端面经-webpack篇--定义、配置、构建流程、 Loader、Tree Shaking、懒加载与预加载、代码分割、 Plugin 机制
前端·webpack·node.js
Yvonne爱编码1 小时前
CSS- 4.4 固定定位(fixed)& 咖啡售卖官网实例
前端·css·html·状态模式·hbuilder
SuperherRo2 小时前
Web开发-JavaEE应用&SpringBoot栈&SnakeYaml反序列化链&JAR&WAR&构建打包
前端·java-ee·jar·反序列化·war·snakeyaml
大帅不是我2 小时前
Python多进程编程执行任务
java·前端·python
前端怎么个事2 小时前
框架的源码理解——V3中的ref和reactive
前端·javascript·vue.js
Ciito2 小时前
将 Element UI 表格元素导出为 Excel 文件(处理了多级表头和固定列导出的问题)
前端·vue.js·elementui·excel
不爱吃饭爱吃菜3 小时前
uniapp微信小程序一键授权登录
前端·javascript·vue.js·微信小程序·uni-app