我使用的是带有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

我该怎么解决呢?


当前回答

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

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

其他回答

在导入中添加'NgbModalModule',并在entryComponents App.module.ts中添加组件名称,如下所示

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

declarations: [
    AppComponent,
    ExampleDialogComponent
  ],

  entryComponents: [
    ExampleDialogComponent
  ],

如果您仍然没有解决这个问题-像我一样-这是导致我这个错误的原因。正如@jkyoutsey所建议的,我确实在尝试创建一个模态组件。根据他的建议,我花了相当多的时间将我的组件移动到一个服务并将其注入到页面组件中。还将providedIn: 'root'更改为MyModule,当然必须至少向上移动一级以避免循环引用。我不确定这些是否真的有必要。

最终解决8个小时难题的是在我的组件中不实现ngOnInit。我盲目地实现了ngOnInit,即使函数是空的。我倾向于那样做。这可能是一个不好的做法。

无论如何,如果有人能解释为什么空的ngOnInit会导致这个错误,我很乐意阅读评论。

参见entryComponent的详细信息:

如果你正在动态加载任何组件,那么你需要把它放在declarations和entryComponent中:

@NgModule({
  imports: [...],
  exports: [...],
  entryComponents: [ConfirmComponent,..],
  declarations: [ConfirmComponent,...],
  providers: [...]
})

我的错误是在.html中调用NgbModal open方法时参数不正确