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

我该怎么解决呢?


当前回答

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

其他回答

我也有同样的问题。在这种情况下进口[…]]是至关重要的,因为如果你不导入NgbModalModule,它将无法工作。

错误描述说组件应该添加到entryComponents数组中,这是显而易见的,但请确保您已经在第一个位置添加了这个:

imports: [
    ...
    NgbModalModule,
    ...
  ],

如果在应用程序中使用路由

确保在路由路径中添加新组件

例如:

    const appRoutes: Routes = [
  { path: '', component: LoginComponent },
  { path: 'home', component: HomeComponent },
  { path: 'fundList',      component: FundListComponent },
];

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

declarations: [
    AppComponent,
    ExampleDialogComponent
  ],

  entryComponents: [
    ExampleDialogComponent
  ],

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

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

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

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

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

我用的是Angular8,我试着从另一个模块动态地打开这个组件, 在这种情况下,您需要将模块导入到试图打开该组件的模块中,当然,除了导出该组件并将其列出到entryComponents数组中,就像前面的答案所做的那样。

imports: [
    ...
    TheModuleThatOwnTheTargetedComponent,
    ...
  ],