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

我该怎么解决呢?


当前回答

你应该像这样在模块中导入NgbModule:

@NgModule ({ 声明:[ AboutModalComponent ], 进口:[ CommonModule, SharedModule, RouterModule, FormsModule, ReactiveFormsModule, NgxLoadingModule, NgbDatepickerModule, NgbModule ], entryComponents (AboutModalComponent): }) 导出类HomeModule {}

其他回答

参见entryComponent的详细信息:

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

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

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

将此添加到module.ts中,

declarations: [
  AppComponent,
  ConfirmComponent
]

如果ConfirmComponent在另一个模块中,你需要导出它,这样你就可以在外面使用它,添加:

exports: [ ConfirmComponent ]

更新Angular 9或Angular 8,显式启用Ivy

使用Ivy的入口组件不再是必需的,现在已弃用

——用于禁用了Ivy的Angular 9和8——

在动态加载组件的情况下,为了生成ComponentFactory,该组件还必须添加到模块的entryComponents:

declarations: [
  AppComponent,
  ConfirmComponent
],
entryComponents: [ConfirmComponent],

根据entryComponents的定义

指定定义此模块时应编译的组件列表。对于这里列出的每个组件,Angular都会创建一个ComponentFactory,并将其存储在ComponentFactoryResolver中。

我对引导模态也有同样的问题

从 '@ng-bootstrap/ng-bootstrap' 导入 { NgbModal };

如果是这种情况,只需像其他响应建议的那样,将组件添加到模块声明和entryComponents中,但也要将此添加到模块中

import { NgbModule } from '@ng-bootstrap/ng-bootstrap';

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

角6也有同样的问题, 这对我来说很管用:

@NgModule({
...
entryComponents: [ConfirmComponent],
providers:[ConfirmService]

})

如果你有一个像ConfirmService这样的服务,必须在当前模块的提供者中声明,而不是在根模块中声明