【问题标题】:No component factory found for undefined. but did add to @NgModule.entryComponents未找到未定义的组件工厂。但确实添加到@NgModule.entryComponents
【发布时间】:2018-08-07 07:41:13
【问题描述】:

我在 Plunker 的控制台中收到以下错误:

未找到未定义的组件工厂。你把它添加到@NgModule.entryComponents 了吗?

但我确实将组件 (SecondComp) 添加到 @NgModule 中的 entryComponents。请查看此 plnkr 并告诉我,为什么会出现此错误?

https://plnkr.co/edit/BM3NMR?p=preview

//our root app component
import {Component, NgModule, VERSION} from '@angular/core'
import {BrowserModule} from '@angular/platform-browser'
import {FirstComp} from './first.component'
import {SecondComp} from './second.component'
import {InjService} from './inj.service'

@Component({
  selector: 'my-app',
  template: `
    <div>
      <h2>Hello {{name}}</h2>
    </div>
    <first-comp></first-comp>
  `,
})
export class App {
  name:string;
  constructor() {
    this.name = `Angular! v${VERSION.full}`
  }
}

@NgModule({
  imports: [ BrowserModule ],
  providers: [ InjService ],
  declarations: [ App, FirstComp, SecondComp ],
  bootstrap: [ App ],
  entryComponents: [ SecondComp ],
})

export class AppModule {}

谢谢!

【问题讨论】:

  • 嘿伙计,你到底想在这里实现什么?
  • 您好,我正在尝试将 SecondComp 注入 FirstComp 并将其显示在 app.ts 中 FirstComp 的占位符中。
  • 酷。我最近做了一些类似的事情。我让它在没有服务的情况下在 plunker 中工作。可能有帮助。 plnkr.co/edit/XwBmRNZ7ksV9xuA90VpL?p=preview
  • 当我得到 5 个时,我会稍微修改一下这项服务。但现在这可能会有所帮助。
  • 好吧,我明白了。那么使用该服务有什么问题呢?在Angular中这样做是无效的还是这是一个Plunker问题?我之所以这么问,是因为使用这些服务将工作与组件分离会很酷。

标签: angular plunker


【解决方案1】:

您的inj.service.ts 中有一个简单的拼写问题。您将SecondComp 拼写为SecondComponent。将 SecondComponent 更改为 SecondComp 解决了该问题,并取消了 addDynamicComponent 方法中的注释。

inj.service.ts:

之前

import {SecondComponent} from './second.component' // <- Over here

// ...

addDynamicComponent() {
  const factory = this.factoryResolver.resolveComponentFactory(SecondComp)
//  const component = factory.create(this.rootViewContainer.parentInjector)
//  this.rootViewContainer.insert(component.hostView)
}

之后

import {SecondComp} from './second.component' // <- Over here

// ...

addDynamicComponent() {
  const factory = this.factoryResolver.resolveComponentFactory(SecondComp)
  const component = factory.create(this.rootViewContainer.parentInjector)
  this.rootViewContainer.insert(component.hostView)
}

Updated Plnkr

【讨论】:

  • 哇。我会责怪我的鼻窦阻塞错过了这个哈哈。好地方!
  • 谢谢你们!让人们反弹真的很有价值。非常感谢您的回复。 Angular 更难一些,JAVA 会更容易地标记这些拼写错误,必须更加注意。再次感谢!
  • 我投了赞成票,但目前声望不到 15。
  • @user2906774 顺便说一句,您可以单击否决按钮下方的勾号,将此答案标记为已接受。 :)
猜你喜欢
  • 2019-04-10
  • 2019-01-30
  • 1970-01-01
  • 2018-01-06
  • 2019-11-05
  • 2019-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多