vue-router:嵌套命名视图问题。附加组件不可见
vue-router: nested named views problem. Additional component is not visible
我在 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 不可见。
为什么?
当您这样指定路线时,
{
path: '/settings',
// You could also have named views at tho top
components: {
default: UserSettings,
test: TestComponent
}
}
UserSettings
和 TestComponent
被认为是同级组件,因此在渲染时,它会在您在顶部指定的默认 router-view
旁边寻找命名的 router-view
-most div 即 #app
.
However, you put <router-view name="test" />
inside UserSettings
template so it didn't find the router-view
to render TestComponent
to.
要解决此问题,请将 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。
我在 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 不可见。 为什么?
当您这样指定路线时,
{
path: '/settings',
// You could also have named views at tho top
components: {
default: UserSettings,
test: TestComponent
}
}
UserSettings
和 TestComponent
被认为是同级组件,因此在渲染时,它会在您在顶部指定的默认 router-view
旁边寻找命名的 router-view
-most div 即 #app
.
However, you put
<router-view name="test" />
insideUserSettings
template so it didn't find therouter-view
to renderTestComponent
to.
要解决此问题,请将 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。