有几个问题:
首先,注意header.component.html
's 的内容:
<div>
<router-outlet></router-outlet>
</div>
<div>
<router-outlet name="aux"></router-outlet>
</div>
和header
组件的路线:
const routes: Routes = [
{
path: '',
component: HeaderComponent,
children: [
{ path: '', redirectTo: 'secondary', pathMatch: 'full' },
{
path: 'secondary',
loadChildren: () =>
import('./../secondary/secondary.module').then(
(m) => m.SecondaryModule
),
},
],
},
];
组件视图想要显示的内容与路由配置描述的内容不匹配。根据经验,组件视图中的内容X
必须与组件X
在路由配置中所需的内容相对应。在这种情况下,header
组件的视图需要一个命名的 outlet,aux
但是在路由配置中只有主 outlet的路径(即secondary
)。
因此,如果您希望您的组件处理多个插座,您可以执行以下操作:
// header.component route configuration
const routes: Routes = [
{
path: '',
component: HeaderComponent,
children: [
{ path: '', redirectTo: 'secondary', pathMatch: 'full' },
{
path: 'secondary',
loadChildren: () =>
import('./../secondary/secondary.module').then(
(m) => m.SecondaryModule
),
},
// !
{
path: 'foo',
outlet: 'aux',
component: FooComponent,
},
],
},
];
该navigate()
方法如下所示:
navigate() {
this.router.navigate([
"header",
{
// key-value pairs
// `key`: the **name** of the outlet
// `value`: the paths defined for the outlet
outlets: {
primary: ["secondary", "abc"],
aux: ["foo"]
}
}
]);
}
StackBlitz 演示。
此外,如果您想了解更多有关 Angular 路由器的信息,我建议您查看: