vue3踩坑记录(一) vue-router4的404路由匹配规则

{
    path: '*',
    name: 'error',
    component: Error,
    meta: { title: '404' },
}

区别于上面旧版的匹配规则,新版vue-router4需要使用自定义的路径参数正则表达式,在路径参数后面的括号中加入正则表达式;

const routes = [
  // 将匹配所有内容并将其放在 `$route.params.pathMatch` 下
  { path: '/:pathMatch(.*)*', name: 'NotFound', component: NotFound },
  // 将匹配以 `/user-` 开头的所有内容,并将其放在 `$route.params.afterUser` 下
  { path: '/user-:afterUser(.*)', component: UserGeneric },
]

即需要修改成:

{
    path: '/:pathMatch(.*)',
    name: 'error',
    component: Error,
    meta: { title: '404' },
},

 

posted @ 2022-01-27 11:43  隔壁的小明  阅读(2962)  评论(0)    收藏  举报