我有一个带有路由器出口的根应用程序组件,并且路由是从主模块路由加载的,该路由在其子路由中使用带有 loadchildren 的延迟加载。在 home 组件中有一个 router-outlet,在 home 的所有子模块中都有 router-outlet,它们是延迟加载的。路由工作正常,但子路由也被加载到根路由器出口中。例如:- 组件“testCreateComponent”正在加载 localhost:4200/test/create 以及 localhost:4200/create。
示例代码级别如下:-
app.component.html
<div>app</div>
<router-outlet></router-outlet>
应用程序路由.module.ts
export const APP_ROUTES: Routes = [
{path: '**', redirectTo: '', pathMatch: 'full'}
];
home.component.html
<div>home</div>
<router-outlet><router-outlet>
家庭路由.module.ts
const routes: Routes = [{
path: '',
component: HomeComponent,
canActivate: [LoggedUserGuard],
canActivateChild: [AuthGuard],
children: [
{path: '', redirectTo: 'dashboard', pathMatch: 'full'},
{
path: 'test',
loadChildren: () => testModule
},
{
path: 'dashboard',
component: DashboardComponent,
data: {
breadcrumb: 'Dashboard'
}
},
{
path: 'dummy',
loadChildren: () => dummyModule,
}
]
}];
测试组件.html
<div>Test</test>
<router-outlet></router-outlet>
测试路由.module.ts
const routes: Routes = [{
path: '',
component: TestComponent,
children: [
{path: '', component: ListComponent,
data: {
breadcrumb: 'List'
}},
{path: 'create', component: testCreateComponent},
{ path: 'detail/:id',
component: TestEditComponent,
}
]
}];