[英]Angular: Lazy load a child module and inject its main component in the View of the Parent home component
I have a parent module where i'm lazy loading a child module 我有一个父模块,在这里我懒于加载子模块
As you may see , i don't import my child module directly , but i'm loading it lazily under routing file : 如您所见,我没有直接导入我的子模块,但是我在路由文件下懒洋洋地加载了它:
Parent Module : 父模块:
@NgModule({
imports: [
CommonModule,
ParentRoutingModule,
],
exports: [
ParentRoutingModule
],
declarations: [ParentComponent]
})
export class ParentModule { }
Parent Routing Module : 父路由模块:
const parentRoutes: Routes = [
{path: '', component: ParentHomeComponent},
{
path: '',
loadChildren: () => ChildModule,
canLoad: [ChildModuleGuard]
},
];
@NgModule({
imports: [
CommonModule,
RouterModule.forChild(parentRoutes)
],
declarations: [],
exports: [
RouterModule
]
})
export class ParentRoutingModule { }
In my main ParentModule component ; 在我的主要ParentModule组件中; the ParentHomeComponent i want to inject the view of my ChildHomeCOmponent , like this :
在ParentHomeComponent我想注入我ChildHomeCOmponent认为,像这样的:
<p>
Parent-home works!
</p>
<app-Child-home></app-Child-home>
but this throws an error telling that the ChildHomeComponent isn't part of the ParentModule (as i'm not importing it directly) ;
但这引发了一个错误,告诉您ChildHomeComponent不是ParentModule的一部分(因为我没有直接导入它);
How to deal with that ?? 如何处理?
I want that if the module is loaded , my child home componen t gets displayed under the parent home component 我希望如果模块已加载 ,则我的孩子家庭组件会显示在父家庭组件下
Suggestions ?? 建议??
This is not how lazy loading works in Angular. 这不是Angular中的延迟加载方式。
Non-lazy loaded modules cannot access components from lazy loaded modules. 非延迟加载的模块无法访问延迟加载的模块中的组件。
This is how you should lazy load you child module: 这是应该延迟加载子模块的方式:
const parentRoutes: Routes = [
{path: '', component: ParentHomeComponent},
{
path: '',
loadChildren: 'path/ChildModule#ChildModule',
canLoad: [ChildModuleGuard]
},
];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.