我在vue-router文档中找到了这个示例:
const router = new VueRouter({
mode: 'history',
routes: [{
path: '/settings',
// You could also have named views at tho top
component: UserSettings,
children: [{
path: 'emails',
component: UserEmailsSubscriptions
}, {
path: 'profile',
components: {
default: UserProfile,
helper: UserProfilePreview
}
}]
}]
})
我试图做这样的事情:
https://jsfiddle.net/pt9o6h8e/
const router = new VueRouter({
mode: 'history',
routes: [{
path: '/settings',
components: {
default: UserSettings,
test: TestComponent
}
children: [{
path: 'emails',
component: UserEmailsSubscriptions
}, {
path: 'profile',
components: {
default: UserProfile,
helper: UserProfilePreview
}
}]
}]
})
,但是TestComponent不可见。 为什么?
答案 0 :(得分:0)
当您这样指定路线时,
{
path: '/settings',
// You could also have named views at tho top
components: {
default: UserSettings,
test: TestComponent
}
}
UserSettings
和TestComponent
被视为同级组件,因此在渲染时,它将在最顶层指定的默认router-view
旁边查找一个命名为router-view
的元素。 div为#app
。
但是,您将
<router-view name="test" />
放进了里面UserSettings
模板,因此找不到router-view
来渲染TestComponent
要解决此问题,请将test
路由器视图从UserSettings
移到#app
。
<div id="app">
<h1>Nested Named Views</h1>
<router-view></router-view>
<router-view name="test" class="us__content us__content--helper"/>
</div>
或者,如果您想将其保留在UserSettings
中,请通过在任意一条子路径中移动test: TestComponent
来更改声明方式。
请参见working example。