事情是这样的,虽然别名现在很好地满足了我的需要,但是我想知道如何为一个路径声明多个别名,那么,类似的东西会起作用吗?示例:
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN']
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
}
]
}) 我的意思是,这是否建议的做法?在Vue路由器中有更好的实践吗?
发布于 2018-10-19 16:36:59
这很好,他们甚至有一个官方例子这样做。
const router = new VueRouter({
mode: 'history',
base: __dirname,
routes: [
{ path: '/root', component: Root, alias: '/root-alias' },
{ path: '/home', component: Home,
children: [
// absolute alias
{ path: 'foo', component: Foo, alias: '/foo' },
// relative alias (alias to /home/bar-alias)
{ path: 'bar', component: Bar, alias: 'bar-alias' },
// multiple aliases
{ path: 'baz', component: Baz, alias: ['/baz', 'baz-alias'] },
// default child route with empty string as alias.
{ path: 'default', component: Default, alias: '' },
// nested alias
{ path: 'nested', component: Nested, alias: 'nested-alias',
children: [
{ path: 'foo', component: NestedFoo }
]
}
]
}
]
});如果您更担心拼写错误,那么您可能只需在*通配符路径上使用导航保护,该通配符基于路由路径的子字符串重定向。
发布于 2021-09-01 09:41:57
实际上,你所做的应该是完全好的。您可以提供一个别名数组,vue将理解。
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN'] // multiple aliases
},
]
}) (我复制了您的代码,因为我认为这确实是答案)。
https://stackoverflow.com/questions/52896318
复制相似问题