vue3设置路由path:‘*‘报错解决方法

一般我们再设置404页面路由的时候是这样的:

{
path: "/404",
name: "notFound",
component: notFound
}, {
path: "*", // 此处需特别注意置于最底部
redirect: "/404"
}

但是如果你使用的是vue3的话,你会遇到这样的一个报错:

Catch all routes (“*”) must now be defined using a param with a custom regexp.

这个报错翻译过来就是捕获所有路由(“*”)现在必须使用带有自定义regexp的参数定义

首先我们得知道在vue3中对404配置进行了修改,必须要使用正则匹配

那么这个时候我们就需要用到这两个属性值:

{
path:'/:pathMatch(.*)*'
}
{
path:'/:pathMatch(.*)'
}

官方代码:

// plan on directly navigating to the not-found route using its name
  { path: '/:pathMatch(.*)*', name: 'not-found', component: NotFound },
  // if you omit the last `*`, the `/` character in params will be encoded when resolving or pushing
  { path: '/:pathMatch(.*)', name: 'bad-not-found', component: NotFound },

那么便知道了这两种写法的不同:

如果省略最后一个’*‘在解析或推送时将对params中的’/'字符进行编码

借鉴文章: https://blog.csdn.net/qq_42723113/article/details/119846710

  • 10
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值