Reputation: 1537
The thing is the following, while alias for now covers my needs perfectly, I wonder how to declare multiple aliases for a path, so, would something like this work? Example:
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN']
},
{
path: '/about',
name: 'about',
component: () => import('./views/About.vue')
}
]
})
By this I mean, is it the recommended way to do so? Is there a better practice for that in Vue router?
Upvotes: 9
Views: 6109
Reputation: 568
Actually , what you did should be totally fine. You can provide an array of aliases and vue will understand.
export default new Router({
routes: [
{
path: '/',
name: 'home',
component: Home,
alias: ['/home', '/home2', '/homeN'] // multiple aliases
},
]
})
(I copied your code as I believe it is indeed the answer) .
Upvotes: 1
Reputation: 9362
This is fine, they even have an official example doing it.
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 }
]
}
]
}
]
});
If you're more worried about misspellings then you could potentially just use a navigation guard on a *
wildcard path that redirects based on substrings of the route path.
Upvotes: 7