我在Plunker的控制台中遇到以下错误:
没有找到不确定的组件工厂。您是否将其添加到 @ngmodule.entrycomponents?
,但我确实将组件(二次COMP(添加到@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 {}
谢谢!
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)
}
更新了PLNKR