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

我该怎么解决呢?


当前回答

将该组件添加到应用模块的@NgModule的entryComponents中:

entryComponents:[ConfirmComponent],

以及声明:

declarations: [
    AppComponent,
    ConfirmComponent
]

其他回答

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

 entryComponents: [
CityModalComponent
],

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

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

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

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

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

例如:

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

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

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

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