当前位置: 首页 > article >正文

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

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

文章目录

  • Vue3中路由配置Catch all routes ("*") must .....问题
  • 1. 业务场景描述
    • 1. 加载并添加异步路由场景
    • 2. vue2中加载并添加异步路由(OK)
    • 3. 转vue3后不好使(Error)
      • 1. 代码
      • 2. 错误
  • 2. 处理方式
    • 1. 修改前
    • 2. 修改后
    • 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中路由导航守卫中加载动态路由案例代码如下

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. 代码

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. 错误

在这里插入图片描述

详细信息如下

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. 修改前

 asyncRouter.push({       
    component: "error/404",
    name: "404",
     path: "*"
 });

2. 修改后

 asyncRouter.push({       
    component: "error/404",
    name: "404",
    path: "/:catchAll(.*)"
 });

3. vue3中完整代码案例

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})
}

http://www.kler.cn/news/233381.html

相关文章:

  • 通过Harbor构建docker私服仓库
  • 前端使用pdf.js进行pdf文件预览的第二种方式:Viewer.html
  • Quartus工程的qsf配置约束文件介绍
  • 【C语言】一道相当有难度的指针某大厂笔试真题(超详解)
  • 106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)
  • RTE2023第九届实时互联网大会:揭秘未来互联网趋势,PPT分享引领行业新思考
  • Python基础篇_修饰符(Decorators)【下】
  • 常用的正则表达式
  • 一条 SQL 查询语句是如何执行的
  • 探索Spring Validation:优雅实现后端数据验证的艺术
  • 数据结构-->线性表-->单链表
  • JAVA面试题12
  • 信号——block+pending+handler表
  • C语言常见面试题:什么是常量?C语言中有哪些类型的常量?
  • Python 小白的 Leetcode Daily Challenge 刷题计划 - 20240209(除夕)
  • 初识文件包含漏洞
  • 【OpenHarmony硬件操作】风扇与温湿度模块
  • 【Spring】Spring 对 Ioc 的实现
  • kaggle实战语义分割-Car segmentation(附源码)
  • 数据库管理phpmyadmin
  • Linux C/C++ 原始套接字:打造链路层ping实现
  • 【ESLint】TypeError:this.libOptions.parse is not a function
  • 【从Python基础到深度学习】4. Linux 常用命令
  • MinMaxScaler, StandardScaler数据预处理中常用的两种缩放方法,用于将数据标准化或归一化到特定的范围或分布
  • 【CV论文精读】EarlyBird: Early-Fusion for Multi-View Tracking in the Bird’s Eye View
  • IOS破解软件安装教程
  • 达梦数据库适配Springboot+MybatisPlus+达梦数据库
  • 谷歌 DeepMind 联合斯坦福推出了主从式遥操作双臂机器人系统增强版ALOHA 2
  • 嵌入式单片机中晶振的工作原理
  • laravel distinct查询问题,laravel子查询写法