我使用的是带有webpack的Angular 4模板 当我尝试使用一个组件(ConfirmComponent)时,我有这个错误:

没有为ConfirmComponent找到组件工厂。你加进去了吗 @NgModule.entryComponents吗?

该组件在app.module.server.ts中声明

@NgModule({
  bootstrap: [ AppComponent ],
  imports: [
    // ...
  ],
  entryComponents: [
    ConfirmComponent,
  ],
})
export class AppModule { }

还有app。module。browser。ts和app。module。shared。ts

我该怎么解决呢?


当前回答

我导入材料设计对话框模块,所以我创建了aboutcomponent的对话框,从openDialog方法调用这个组件,然后我得到了这个错误,我只是把这个

declarations: [
    AppComponent,
    ExampleDialogComponent
  ],

  entryComponents: [
    ExampleDialogComponent
  ],

其他回答

我在Angular7中创建动态组件时也遇到了同样的问题。有两个组件(TreatListComponent, MyTreatComponent)需要动态加载。我只是在我的app.module.ts文件中添加了entryComponents数组。

    entryComponents: [
    TreatListComponent,
    MyTreatComponent
  ],

在本节中,你必须在app.module.ts文件的以下部分中输入作为子组件使用的组件:[CityModalComponent](模态组件):

 entryComponents: [
CityModalComponent
],

我在使用动态组件的ag网格中遇到了同样的问题。我发现你需要在ag-grid模块中添加动态组件。withcomponents []

进口:[ StratoMaterialModule, BrowserModule, AppRoutingModule, HttpClientModule, BrowserAnimationsModule, NgbModule.forRoot (), FormsModule, ReactiveFormsModule, AppRoutingModule, AgGridModule.withComponents (ProjectUpdateButtonComponent) ],

我可能稍后再回复。但是对于那些仍然在寻找这个问题的解决方案并且在他们的代码中有这个的人来说,删除这个可能是有帮助的。我们在tsconfig中有以下条目。json文件:

  "angularCompilerOptions": {
    "enableIvy": false
  }

我们也面临同样的问题。经过大量的实验,我们从tsconfig.json中删除了这个块。现在我们的代码不再抱怨这个问题了。

在这里澄清一下。如果你不直接在组件中使用ComponentFactoryResolver,并且你想将它抽象为服务,然后将其注入到组件中,你必须在该模块的providers下加载它,因为如果延迟加载它将不起作用。