我可以在 Vue 路由器中为特定路由设置多个别名吗?
May I have multiple alias in Vue's router for an specific route?
事情是这样的,虽然别名现在完全满足了我的需求,但我想知道如何为一个路径声明多个别名,那么,这样的事情行得通吗?示例:
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN']
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
}
]
})
我的意思是,这是推荐的方法吗?在 Vue 路由器中有更好的做法吗?
这很好,他们甚至有一个 official example 在做这件事。
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 }
]
}
]
}
]
});
如果您更担心拼写错误,那么您可以在 *
通配符路径上使用导航守卫,该路径基于路由路径的子字符串进行重定向。
实际上,你所做的应该完全没问题。你可以提供一组别名,vue 会理解。
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN'] // multiple aliases
},
]
})
(我复制了你的代码,因为我相信它确实是答案)。
事情是这样的,虽然别名现在完全满足了我的需求,但我想知道如何为一个路径声明多个别名,那么,这样的事情行得通吗?示例:
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN']
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
}
]
})
我的意思是,这是推荐的方法吗?在 Vue 路由器中有更好的做法吗?
这很好,他们甚至有一个 official example 在做这件事。
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 }
]
}
]
}
]
});
如果您更担心拼写错误,那么您可以在 *
通配符路径上使用导航守卫,该路径基于路由路径的子字符串进行重定向。
实际上,你所做的应该完全没问题。你可以提供一组别名,vue 会理解。
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN'] // multiple aliases
},
]
})
(我复制了你的代码,因为我相信它确实是答案)。