【发布时间】:2021-08-18 14:17:33
【问题描述】:
尝试使用 vue-router 向 url 添加语言前缀,但返回错误消息:Uncaught Error: Missing required param "locale" 我正在尝试添加支持语言前缀的路由,如 en、de、nl、fr 等上...
我确实看到我正在使用我猜实际上不需要的子路由......!
example.com/en/users
example.com/nl/users
example.com/fr/users
routes.js
const routes = [
{
path: "/",
redirect: 'en'
},
{
path: '/:locale',
//path: '/:locale?', // this will work but it wont render the component
name:'locale',
component: {
template: '<router-view />'
},
children:[
{ path: 'welcome', name: 'dashboard', component: page('Dashboard.vue'),
},
{ path: 'users/index', name: 'users.index', component: page('Users/Index.vue'),
children:[
{ path: 'create', name: 'users.create', component: page('Users/Create.vue') }
]
},
]
},
]
router.js
const router = createRouter({
history: createWebHistory(),
routes,
base: process.env.BASE_URL,
});
router.beforeEach((to, from, next) => {
next();
})
【问题讨论】:
-
path: ':locale',怎么样 -
未捕获的错误:路由路径应以“/”开头:“:locale?”应该是“/:locale?”。
标签: vue-router vuejs3