我有多个项目块,类似于下面jsfiddle中的书籍。每本书都有一个子菜单,用户可以点击查看特定的书籍内容。
问题是当用户访问book1 / summary时,所有其他书籍也将切换到摘要组件。我不希望这样,我希望所有其他书籍都独立存在。例如: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'
}
})
答案 0 :(得分:3)
您应该可以使用 CHILD ROUTES 使其正常工作,但您需要稍微更改一下设置。
每本书都需要单独导航并在其中<router-view>
内部,然后在特定书中调用子路径应该有效。
尝试引用文档:https://router.vuejs.org/en/essentials/nested-routes.html