我正在尝试“插入”包含路由配置(从RouterModule.forChild()
导入)的子ngModule(特征模块)到父ngModule中。
使用延迟加载时,使用父模块路由配置中的loadChildren
密钥指定“插入”子模块的位置。
例如:
export const parentModuleRouteConfig = [{
path: 'myfeaturemodule',
loadChildren: '/app/child.module'
}];
实际上,我不想使用延迟加载。
如何告诉路由器“插入”(或使用)给定路径的子模块中指定的路由配置?
导出子路由,而不是将其添加到子模块。
export const ChildRoutes: Routes = [
{ path: '', component: ChildComponent }
];
将子模块导入父模块并直接包含路由。
const parentModuleRouteConfig: [{
path: 'myfeaturemodule',
// loadChildren: () => ChildModule
children: ChildRoutes
}];
@NgModule({
imports: [
ChildModule
]
})
export class ParentModule { }
您仍然可以使用loadChildren同步加载。有一些例子in this github issue。
import { ChildModule } from '/app/child.module';
export const parentModuleRouteConfig = [{
path: 'myfeaturemodule',
loadChildren: () => ChildModule
}];
您可以定义预加载策略,告诉Angular预加载所有模块:
import { ...., PreloadAllModules } from '@angular/router'
@NgModule({
....
imports: [
RouterModule.forRoot(arrayOfYourRoutes, { preloadingStrategy: PreloadAllModules })
]
如果要仅预加载某些模块,还可以定义自定义策略。要获得这样做的好方法,请检查:example in angular doc
在评论后编辑,自我描述您的模块,这是您可以做的:
@NgModule(
imports: [RouterModule.forChild(routesOfFeatureModule)], //use forChild instead of forRoot
exports: [RouterModule]
)
export class MyFeatureRoutingModule{} //define routing in a separate module of your feature, everything related to routing go there. This way, you doen't polute your main FeatureModule file. Re-export RouterModule to be able to use router directives in your FeatureModule just by importing MyFeatureRoutingModule
@NgModule(
...
imports: [MyFeatureRoutingModule] //import routing of your feature module
)
export class MyFeatureModule{}
@NgModule(
...
imports: [MyFeatureModule] // import your feature module
)
export class AppModule{}