Angular 4:未找到组件工厂,您是否将其添加到 @NgModule.entryComponents 中? [英] Angular 4: no component factory found,did you add it to @NgModule.entryComponents?

查看:45
本文介绍了Angular 4:未找到组件工厂,您是否将其添加到 @NgModule.entryComponents 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 webpack 中使用 Angular 4 模板当我尝试使用组件 (ConfirmComponent) 时出现此错误:

I'm using Angular 4 template with webpack and I have this error when I try to use a component (ConfirmComponent):

未找到 ConfirmComponent 的组件工厂.你把它添加到@NgModule.entryComponents?

No component factory found for ConfirmComponent. Did you add it to @NgModule.entryComponents?

组件在app.module.server.ts

@NgModule({
  bootstrap: [ AppComponent ],
  imports: [
    // ...
  ],
  entryComponents: [
    ConfirmComponent,
  ],
})
export class AppModule { }

我还有 app.module.browser.tsapp.module.shared.ts

我该如何解决?

推荐答案

在你的 module.ts 中添加这个,

Add this in your module.ts,

declarations: [
  AppComponent,
  ConfirmComponent
]

如果ConfirmComponent在另一个模块中,你需要将它导出到那里以便你可以在外面使用它,添加:

if ConfirmComponent is in another module, you need to export it there thus you can use it outside, add:

exports: [ ConfirmComponent ]

---在显式启用 Ivy 的情况下更新 Angular 9 或 Angular 8---

Ivy 的入口组件不再需要,现在已弃用

---对于禁用常春藤的 Angular 9 和 8---

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

In the case of a dynamically loaded component and in order for a ComponentFactory to be generated, the component must also be added to the module’s entryComponents:

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

根据entryComponents的定义

according to the definition of entryComponents

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

Specifies a list of components that should be compiled when this module is defined. For each component listed here, Angular will create a ComponentFactory and store it in the ComponentFactoryResolver.

这篇关于Angular 4:未找到组件工厂,您是否将其添加到 @NgModule.entryComponents 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆