【问题标题】:No component factory found for undefined. but did add to @NgModule.entryComponents with SharedModule未找到未定义的组件工厂。但确实使用 SharedModule 添加到 @NgModule.entryComponents
【发布时间】:2019-04-10 07:40:41
【问题描述】:

所以我找到了几十篇解释这个问题的文章,大多数建议都说将我的组件添加到 @NgModule 中的 entryComponents 数组中,但我对我拥有的许多模块中的哪个或哪些 entryComponents 感到困惑.. 所以基本上我的模块依赖项如下所示:

AppModule
 - EnvironmentModule
 - ContainerModule
 - SharedModule

EnvironmentModule
 - ContainerModule
 - EnvironmentListItemDetailComponent
    private _dialogRef: MatDialogRef<ConfirmDialogComponent>;

ContainerModule
 - MatDialogModule, 
 - ConfirmDialogComponent (this is the component that I want to move)
 - ContainerListItemDetailComponent
     private _dialogRef: MatDialogRef<ConfirmDialogComponent>;

基本上,我想将 ConfirmDialogComponent 从嵌套子模块移动到环境和容器模块都可以依赖的共享模块。 (我也很想将 ContainerModule 从 EnvironmentModule 下移出,但那是另一天的事了)

所以我想把 ConfirmDialogComponent 移到 SharedModule 中,然后把所有东西都连接起来,App、Environment、Container、Shared 的 @NgModule 是什么样的?我真的很困惑..所以这是我到目前为止所拥有的:

shared.module.ts:

@NgModule({
  imports: [
    CommonModule,
    MatDialogModule,
    BrowserAnimationsModule
  ],
  declarations: [
    ConfirmDialogComponent
  ],
  exports: [
    ConfirmDialogComponent
  ],
  entryComponents: [
    ConfirmDialogComponent
  ]
})
export class SharedModule { }

environment.module.ts:

@NgModule({
  imports: [
    HttpClientModule,
    CommonModule,
    FormsModule,
    EnvironmentRoutingModule,
    ContainerModule
  ],
  declarations: [
    EnvironmentListComponent,
    EnvironmentListEnvironmentsComponent,
    EnvironmentListItemDetailComponent,
    EnvironmentListItemComponent
  ],
  providers: [... ]
})

container.module.ts:

@NgModule({
  imports: [
    FormsModule,
    CommonModule,
    BrowserAnimationsModule,
    MatDialogModule,
    ContainerRoutingModule
  ],
  declarations: [
    KeysPipe,
    ContainerListComponent,
    ContainerListItemComponent,
    ContainerListItemDetailComponent,
    ParameterListComponent,
    ParameterListItemComponent,
    ParameterTypeInfoComponent,
    ConfirmDialogComponent
  ],
  exports: [
    ContainerListComponent
  ],
  providers: [...],
  entryComponents: [ ConfirmDialogComponent ]
})

app.module.ts:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    FormsModule,
    BrowserModule,
    AppRoutingModule,
    HttpModule,
    ContainerModule,
    EnvironmentModule,
    LoginModule,
    SharedModule,
    PageNotFoundModule /* DO NOT MOVE THIS - as a result of routing peculiarities the order of child routes matter for handling wildcard ** https://stackoverflow.com/questions/40015385/angular-2-router-wildcard-handling-with-child-routes */
  ],
  providers: [
    AppConfigService,
    AuthGuardService,
    BootstrapService,
    EventBrokerService,
    HttpClientService,
    TruIdTokenService,
    StartupService,
    {
      provide: APP_INITIALIZER,
      useFactory: initConfiguration,
      deps: [StartupService],
      multi: true
    }
  ],
  bootstrap: [AppComponent]
})

所以我不知道:\ 这有点乱,我什至不确定我的所有依赖项是否组织正确。

【问题讨论】:

  • 只需从您的子模块中删除入口组件(ConfirmDialogComponent),例如 ContainerModuleEnvironmentModule ,我认为它会起作用

标签: angular angular-components angular-module angular-cli-v6


【解决方案1】:

所以我想我需要:

  1. 删除所有条目entryComponents
  2. 从除SharedModule 之外的所有模块中删除ConfirmDialogComponent 的声明。
  3. SharedModule 导入到使用ConfirmDialogComponent 的模块中。
  4. entryComponents: [ ConfirmDialogComponent ] 添加到SharedModule
  5. 在任何子组件中更改import { ConfirmDialogComponent } 以使用共享组件的新位置

【讨论】:

    【解决方案2】:
    1. 删除所有条目entryComponents
    2. 从除SharedModule 之外的所有模块中删除ConfirmDialogComponent 的声明。
    3. SharedModule 导入到使用ConfirmDialogComponent 的模块中。

    【讨论】:

    • Nope.. RROR 错误:没有为 ConfirmDialogComponent 找到组件工厂。你把它添加到@NgModule.entryComponents 了吗? EnvironmentListItemDetailComponent.push../src/app/environment/environment-list-item-detail/environment-list-item-detail.component.ts.EnvironmentListItemDetailComponent.confirmSave (environment-list-item-detail.component.ts:192)在 Object.eval [as handleEvent] (EnvironmentListItemDetailComponent.html:34) at handleEvent (core.js:10251) at callWithDebugContext (core.js:11344)
    • SharedModule导入environment-list-item-detail.component.tsModule
    • 是的,我这样做了:@NgModule({ 导入:[ HttpClientModule, CommonModule, FormsModule, EnvironmentRoutingModule, ContainerModule, SharedModule ],声明:[ EnvironmentListComponent, EnvironmentListEnvironmentsComponent, EnvironmentListItemDetailComponent, EnvironmentListItemComponent ],
    • 修复了!我不得不将 entryComponents: [ ConfirmDialogComponent ] 添加到 SharedModule 中,然后愚蠢地忘记更改子导入的组件导入: import { ConfirmDialogComponent } from '../../shared/confirm-dialog/confirm-dialog.组件';
    猜你喜欢
    • 2018-08-07
    • 2019-01-30
    • 1970-01-01
    • 2018-01-06
    • 2019-11-05
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多