我有多个项目块,类似于下面的JSFIDDLE中的书籍。每本书都有一个子菜单,用户可以单击以查看特定的书籍内容。
问题是用户访问book1/摘要时,所有其他书籍也将切换到摘要组件。我不想要那个,我希望所有其他书籍都独立留在它们的位置。例如:Book1显示摘要,Book2显示了条件,Book3显示了预订,并且每个人都独立于其他。
有人可以帮忙吗?非常感谢。
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'
}
})
您应该能够使用子路由使其正常工作,但是您需要稍微更改设置。
每本书都需要内部单独的导航,其中的<router-view>
需要在特定书内拨打子路由。
尝试参考文档:https://router.vuejs.org/en/esentials/nested-routes.html