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

我该怎么解决呢?


当前回答

在我的例子中,我根本不需要entryComponents -只需要下面链接中描述的基本设置,但我需要在主应用程序模块和外围模块中都包含导入。

imports: [
    NgbModule.forRoot(),
    ...
]

https://medium.com/@sunilk/getting-started-angular-6-and-ng-bootstrap-4-4b314e015c1c

如果一个组件将被包含在模态中,你只需要将它添加到entryComponents中。从文档中可以看出:

您可以将现有组件作为模式窗口的内容传递。在 在这种情况下,请记住将内容组件添加为entryComponents 部分。

其他回答

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

此错误发生在您尝试动态加载组件时,并且:

要加载的组件不是路由模块 该组件在模块entryComponents中为no。

在routingModule

const routes: Routes = [{ path: 'confirm-component', component: ConfirmComponent,data: {}}]

或者在模块中

entryComponents: [
ConfirmComponent
} 

要修复此错误,您可以将路由器添加到组件或添加到模块的entryComponents中。

添加路由器到组件。这种方法的缺点是你的 组件将通过该url访问。 将其添加到entryComponents中。在这种情况下,您的组件将没有附加任何url,它将无法通过url访问。

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

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

    entryComponents: [
    TreatListComponent,
    MyTreatComponent
  ],

在我的情况下,我解决了这个问题: 1)将NgxMaterialTimepickerModule放在app模块导入中:[] 2)在testmodule中放置NgxMaterialTimepickerModule。Ts导入:[] 3)将testcomponent放在声明中:[]和entryComponents:[]

(我使用的是angular 8+版本)