Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
179
rated 0 times [  183] [ 4]  / answers: 1 / hits: 38790  / 7 Years ago, fri, january 5, 2018, 12:00:00

I have a Vue.js single page application where there is a main navbar that uses <router-view/> to render different pages.



Something like this:





<main-header/> <!-- navigation links -->
<transition name=slide-fade mode=out-in>
<router-view/> <!-- different pages -->
</transition>


In one of those pages I have a sidebar that has more navigation links (that are using <router-link/> just like the main navbar.



Something like this:



<sidebar/> <!-- navigation links -->
<div class=content>
<transition name=slide-fade mode=out-in>
<router-view/> <!-- content beside the sidebar -->
</transition>
</div>


When I click on the sidebar navigation links I want the content beside the sidebar to change as well as the url to change. However, I lose the sidebar, and just get the component that is to be rendered in the content section.



How do I achieve the desired result? How do I use multiple <router-view/>s one of which is inside another component, like the example above?


More From » vue.js

 Answers
14

The reason the sidebar disappeared is all the components are rendered in the first <router-view> besides the <main-header>.



You should use the nested router by configuring children in the sidebar router like:



const router = new VueRouter({
routes: [
{ path: '/your-sidebar-url', component: your-sidebar-page,
children: [
{
// A will be rendered in the second <router-view>
// when /your-sidebar-url/a is matched
path: 'a',
component: A
},
{
// B will be rendered in the second <router-view>
// when /your-sidebar-url/b is matched
path: 'b',
component: B
}
]
}
]
})


More info in nested routes


[#55540] Sunday, December 31, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
cartersinceren

Total Points: 442
Total Questions: 116
Total Answers: 106

Location: San Marino
Member since Thu, Jun 30, 2022
2 Years ago
;