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

2024-02-08 11:12

本文主要是介绍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.dataasyncRouter = res.dataasyncRouter.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.tokenif (token) {dbApi.getRouter({}).then((response) => {const res = response.dataasyncRouter = res.dataasyncRouter.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:16at 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.tokenif (token) {dbApi.getRouter({}).then((response) => {const res = response.dataasyncRouter = res.dataasyncRouter.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})
}

这篇关于Vue3中路由配置Catch all routes (“*“) must .....问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/690842

相关文章

Spring Boot配置和使用两个数据源的实现步骤

《SpringBoot配置和使用两个数据源的实现步骤》本文详解SpringBoot配置双数据源方法,包含配置文件设置、Bean创建、事务管理器配置及@Qualifier注解使用,强调主数据源标记、代... 目录Spring Boot配置和使用两个数据源技术背景实现步骤1. 配置数据源信息2. 创建数据源Be

Python错误AttributeError: 'NoneType' object has no attribute问题的彻底解决方法

《Python错误AttributeError:NoneTypeobjecthasnoattribute问题的彻底解决方法》在Python项目开发和调试过程中,经常会碰到这样一个异常信息... 目录问题背景与概述错误解读:AttributeError: 'NoneType' object has no at

从入门到精通详解LangChain加载HTML内容的全攻略

《从入门到精通详解LangChain加载HTML内容的全攻略》这篇文章主要为大家详细介绍了如何用LangChain优雅地处理HTML内容,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录引言:当大语言模型遇见html一、HTML加载器为什么需要专门的HTML加载器核心加载器对比表二

Spring的RedisTemplate的json反序列泛型丢失问题解决

《Spring的RedisTemplate的json反序列泛型丢失问题解决》本文主要介绍了SpringRedisTemplate中使用JSON序列化时泛型信息丢失的问题及其提出三种解决方案,可以根据性... 目录背景解决方案方案一方案二方案三总结背景在使用RedisTemplate操作redis时我们针对

Spring Boot Maven 插件如何构建可执行 JAR 的核心配置

《SpringBootMaven插件如何构建可执行JAR的核心配置》SpringBoot核心Maven插件,用于生成可执行JAR/WAR,内置服务器简化部署,支持热部署、多环境配置及依赖管理... 目录前言一、插件的核心功能与目标1.1 插件的定位1.2 插件的 Goals(目标)1.3 插件定位1.4 核

RabbitMQ消息总线方式刷新配置服务全过程

《RabbitMQ消息总线方式刷新配置服务全过程》SpringCloudBus通过消息总线与MQ实现微服务配置统一刷新,结合GitWebhooks自动触发更新,避免手动重启,提升效率与可靠性,适用于配... 目录前言介绍环境准备代码示例测试验证总结前言介绍在微服务架构中,为了更方便的向微服务实例广播消息,

Kotlin Map映射转换问题小结

《KotlinMap映射转换问题小结》文章介绍了Kotlin集合转换的多种方法,包括map(一对一转换)、mapIndexed(带索引)、mapNotNull(过滤null)、mapKeys/map... 目录Kotlin 集合转换:map、mapIndexed、mapNotNull、mapKeys、map

nginx中端口无权限的问题解决

《nginx中端口无权限的问题解决》当Nginx日志报错bind()to80failed(13:Permissiondenied)时,这通常是由于权限不足导致Nginx无法绑定到80端口,下面就来... 目录一、问题原因分析二、解决方案1. 以 root 权限运行 Nginx(不推荐)2. 为 Nginx

解决1093 - You can‘t specify target table报错问题及原因分析

《解决1093-Youcan‘tspecifytargettable报错问题及原因分析》MySQL1093错误因UPDATE/DELETE语句的FROM子句直接引用目标表或嵌套子查询导致,... 目录报js错原因分析具体原因解决办法方法一:使用临时表方法二:使用JOIN方法三:使用EXISTS示例总结报错原

Windows环境下解决Matplotlib中文字体显示问题的详细教程

《Windows环境下解决Matplotlib中文字体显示问题的详细教程》本文详细介绍了在Windows下解决Matplotlib中文显示问题的方法,包括安装字体、更新缓存、配置文件设置及编码調整,并... 目录引言问题分析解决方案详解1. 检查系统已安装字体2. 手动添加中文字体(以SimHei为例)步骤