简体   繁体   English

Angular:延迟加载子模块并将其主要组件注入到“父home”组件的“视图”中

[英]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.

相关问题 Angular 从主组件延迟加载模块 - Angular lazy load modules from main component 角度UI路由器-组件-父级-子级-嵌套-视图未加载 - Angular UI-Router - Component - Parent - Child - Nested - View not load 当父数据更改 Angular 4 时,如何强制子组件重新加载其数据及其视图 - How to force child component to reload its data and its view when parent data changes Angular 4 角度路由 - 路由到惰性模块子路径不会启动组件 - Angular routing - routing to a lazy module child path does not initiate the component 父组件中的触发事件来自路由Angular中子组件的加载 - Trigger event in Parent component from child component load in routing Angular Angular 2/4/5 - 将子组件加载到主路由器插座 - Angular 2/4/5 - Load child component to main router-outlet Angular父组件“作用域”子组件 - Angular parent component “scoping” child component 子组件未在主要组件中呈现角度。 当我点击按钮时,它的渲染很容易 - child component is not rendering in main component in angular. its rendering separetly when I click on button Ionic 3 / Angular 4-服务器的延迟加载组件 - Ionic 3 / Angular 4 - Lazy load component from server 刷新Angular 4中的父组件视图 - Refresh the parent component view in Angular 4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM