gpt4 book ai didi

vue-router - Vue Router - 捕获所有通配符不起作用

转载 作者:行者123 更新时间:2023-12-03 15:53:26 34 4
gpt4 key购买 nike

我正在将 Vue Router 与 Vue 3 一起使用,并尝试添加一个包罗万象的路由以在用户尝试访问无效 URL 时重定向用户。当我尝试使用通配符 (*) 时,我将以下错误记录到控制台:

Uncaught Error: A non-empty path must start with "/"
at tokenizePath (vue-router.esm.js?8c4f:975)
at createRouteRecordMatcher (vue-router.esm.js?8c4f:1106)
at addRoute (vue-router.esm.js?8c4f:1190)
at eval (vue-router.esm.js?8c4f:1335)
at Array.forEach (<anonymous>)
at createRouterMatcher (vue-router.esm.js?8c4f:1335)
at createRouter (vue-router.esm.js?8c4f:2064)
at eval (index.js?a18c:26)
at Module../src/router/index.js (app.js:1402)
at __webpack_require__ (app.js:854)
我假设这是因为我没有在包含星号的路径前面加上“/”,但是如果我这样做,那么捕获全部不起作用。以下是我的路线:
imports...

const routes = [
{
path: '/',
name: 'Home',
component: Home
},
{
path: '/user',
name: 'User',
// route level code-splitting
// this generates a separate chunk (user.[hash].js) for this route
// which is lazy-loaded when the route is visited.
component: () => import(/* webpackChunkName: "user" */ '../views/user/User.vue'),
children: [{path: '', component: UserStart}, {path: ':id', component: UserDetail}, {path: ':id/edit', component: UserEdit, name: 'userEdit'}]
},
{path: '/redirect-me', redirect: '/user'},
{path: '*', redirect: '/'}
]

const router = createRouter({
history: createWebHashHistory(),
routes
})

export default router
通配符路由是路由数组中的最后一个对象。有谁知道我做错了什么?

最佳答案

捕获所有路由 (/*) must now be defined使用带有自定义正则表达式的参数:/:catchAll(.*)例如:

    {
// path: "*",
path: "/:catchAll(.*)",
name: "NotFound",
component: PageNotFound,
meta: {
requiresAuth: false
}
}

关于vue-router - Vue Router - 捕获所有通配符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63526486/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com