My routes has the following structure divided into several files:
export const router = new VueRouter({
mode: 'hash',
base: __dirname,
saveScrollPosition: true,
history: true,
routes : Array.concat(userRoutes, siteRoutes)
})
...
// user/routes.js
export const userRoutes = [
{
path: '/user',
name: 'user_main',
component: UserMain,
meta: {authRequired: true},
children: Array.concat([
...
], accountRoutes)
}
]
// account/routes.js
export const accountRoutes = [
{
path: 'account',
name: 'user_account',
component: AccountMain,
children: [
{path: 'edit', name: 'user_edit_account', component: EditAccount},
{path: 'addresses', name: 'user_addresses', component: Addresses},
]
}
]
and i'm trying to catch
/user/account/addresses
but for anything under account/ i get the AccountMain component, not the component that i expected. If i take the addresses component out of account routes and move it to say user/addresses it works. but i can not reach under AccountMain. It is same for any other component under AccountMain
If i try to reach anything that does not exist under account/ for example:
/user/account/blah
it returns empty page for that view.
But, adding
beforeEnter: (to, from, next) => {
to.matched.forEach(m => console.log(m.name))
}
to AccountMain's route definition outputs an extra and the expected name
user_main
user_account
user_addresses
So it finds the name, but returns the parent (AccountMain) instead of it's child component Addresses. This is not related to AccountMain component, since if i remove the AccountMain component from route definition such as the following, i still can not reach the addresses and get an empty page.
export const accountRoutes = [
{
path: 'account',
name: 'user_account',
children: [
{path: 'edit', name: 'user_edit_account', component: EditAccount},
{path: 'addresses', name: 'user_addresses', component: Addresses},
]
}
]
I'm using vue router 2.1.1.
What is it that i'm doing wrong here?
The router-view here is a top-level outlet. It renders the component matched by a top level route. Similarly, a rendered component can also contain its own, nested router-view. More, Nested Routes.
Each parent component need to have its own router-view for its children routes. jsfiddle
Each parent must return it's own router-view , i had the same problem and i did fix it as following :
So in each parent route you have to add this :
and in it's children add a new route with
path: ""
I hope it will help you all for this kind of problems