fir*_*baa 7 javascript lazy-loading typescript angular-ui-router angular
我有一个应用程序,我想 在同一时刻 使用 相同的路由路径延迟加载两个模块。
我的路由模块如下所示:
{
path: 'mypath',
loadChildren: () => HomeModule,
canLoad: [HomeGuard]
},
{
path: 'mypath',
loadChildren: () => AdvisorModule,
canLoad: [AdvisorGuard]
},
Run Code Online (Sandbox Code Playgroud)
但这导致只加载第一个
我无论如何都找不到这样做,例如:
{
path: 'mypath',
loadChildren: () => HomeModule, advisor module // ??
canLoad: [// ??]
},
Run Code Online (Sandbox Code Playgroud)
我也不想在另一个中导入其中一个,像这样,只有一个模块会被延迟加载,另一个会自动加载
怎么可能呢??
您需要将路由重新排列一级,并且还需要为要加载的额外组件添加辅助路由。
这适用于 Angular 9(可能也适用于 8)
{
path: 'home',
component: HostingComponentWithOutlets,
children: [
{
path: 'featureOne',
loadChildren: () => import('xxxxx').then(m => m.FeatureOneModule),
canLoad: [featureOneGuard]
},
{
path: 'featureTwo',
outlet: 'outletAux1',
loadChildren: () => import('yyyyy').then(m => m.FeatureTwoModule),
canLoad: [featureTwoGuard]
},
// you can even load more components to different outlets from the same module
// and update redirectTo and routerLink accordingly
//{
// path: 'featureThree',
// outlet: 'outletAux2',
// loadChildren: () => import('yyyyy').then(m => m.featureTwoModule),
// canLoad: [featureTwoGuard]
//},
{
path: '',
redirectTo:
'/absolute/path/to/home(featureOne/path-to-featureOne-component//outletAux1:featureTwo/path-to-featureTwo-component)',
pathMatch: 'full'
}
]
},
{ path: '', redirectTo: 'home', pathMatch: 'full' }
Run Code Online (Sandbox Code Playgroud)
点击“home”路线将延迟加载所有必需的模块。
在您的HostingComponentWithOutlets
html 模板中,您需要链接到“featureOne”:
<a [routerLink]="featureOne/path-to-featureOne-component"
Run Code Online (Sandbox Code Playgroud)
如果您想使用模板中的辅助路线直接进入完整路线:
<a [routerLink]="['.', { outlets: { 'primary': ['featureOne', 'path-to-featureOne-component'], 'outletAux1': ['featureTwo', 'path-to-featureTwo-component'] } }]"
Run Code Online (Sandbox Code Playgroud)
FeatureOneModule
FeatureTwoModule
应在其等效路由定义中定义“path-to-featureOne-component”并应定义“path-to-featureTwo-component”。
归档时间: |
|
查看次数: |
2720 次 |
最近记录: |