您没有做错任何事情,这只是 angular 路由器中的一个长期存在的错误,在 angular@6.1 中(部分)得到了修复。部分原因是,为了实现您的目标,您必须将应用程序路由中的延迟加载模块配置为未命名路由,而不是在延迟加载模块路由器配置中命名父路由:
app.routes
const routes: Routes = [
{
path: '',
redirectTo: 'home',
pathMatch: 'full'
},
{
/* no name for lazy loaded route */
path: '',
loadChildren: './home/home.module#HomeModule'
},
{
/* you can add multiple empty routes without conflicts */
path: '',
loadChildren: './about/about.module#AboutModule'
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule {}
home.routes
export const homeRoutes: Routes = [
{
/* your lazy loaded module route needs to be named */
path: 'home',
component: HomeComponent,
children: [
{
path: '',
component: HomeListComponent
},
{
path: 'form',
component: HomeFormComponent,
outlet: 'popup'
}
]
}
];
@NgModule({
imports: [RouterModule.forChild(homeRoutes)],
exports: [RouterModule]
})
export class HomeRoutingModule {
}
而您的主组件模板如下所示:
<router-outlet></router-outlet>
<router-outlet name="popup"></router-outlet>
并且routerLink
必须看起来像这样
[routerLink]="['/home', { outlets: { popup: ['form'] } }]"