Reputation: 121
I have multiple blocks of items similar to the books in the jsfiddle below. Each book has a sub-menu and users can click to see a specific book content.
The problem is when users visit book1/summary , all other books will also switch to Summary Component. I don't want that, I want all other books stay at where they are, independently . For example: book1 is showing Summary, book2 is showing Condition, book3 is showing Reservation and each one of them is independent from others.
Can somebody help ? Thanks a lot.
https://jsfiddle.net/trachanh/pL4rmua6/2/
<div id="app">
<router-link to="/">/home</router-link>
<router-link to="/foo">/foo</router-link>
<router-link to="/books">/books</router-link>
<router-view></router-view>
</div>
<template id="books">
<div>
<div class="book" v-for="book in books">
<h3>{{book.title}}</h3>
<ul>
<router-link :to="{ path: '/books/'+book.id+ '/summary'}">Summary</router-link>
<router-link :to="{ path: '/books/'+book.id+ '/location'}">Location</router-link>
<router-link :to="{ path: '/books/'+book.id+ '/condition'}">Condition</router-link>
<router-link :to="{ path: '/books/'+book.id+ '/reservation'}">Reservation</router-link>
</ul>
<router-view></router-view>
</div>
</div>
</template>
const Home = { template: '<div>Home</div>' }
const Foo = { template: '<div>Foo</div>' }
const Books = {
template: '#books',
data: function(){
return {
books: [
{id: 1, title: 'Harry Potter'},
{id: 2, title: 'Twilight'},
{id: 3, title: 'The Hobbit'}
]
}
}
}
const summary = { template: '<div>Summary component</div>' }
const location = { template: '<div>Location component</div>' }
const condition = { template: '<div>Condition component</div>' }
const reservation = { template: '<div>Reservation component</div>' }
const router = new VueRouter({
mode: 'history',
routes: [
{ path: '/', component: Home },
{ path: '/foo', component: Foo },
{ path: '/books', component: Books,
children: [
{
path: ':bookID/summary',
component: summary
},
{
path: ':bookID/location',
component: location
},
{
path: ':bookID/condition',
component: condition
},
{
path: ':bookID/reservation',
component: reservation
}
]
}]
})
new Vue({
router,
el: '#app',
data: {
msg: 'Hello World'
}
})
Upvotes: 7
Views: 12347
Reputation: 13684
You should be able to make it working using CHILD ROUTES, but you would need to slightly change your setup.
Every book needs separate navigation inside and <router-view>
inside it and then calling child route inside the particular book should work.
Try to refer to docs: https://router.vuejs.org/en/essentials/nested-routes.html
Upvotes: 4