这里是我的代码不能在动态创建的组件访问成员属性
模板:
<button (click)='openModal()'>open</button>
.TS
@Component({
...,
entryComponents: [ModalComponent]
})
...
constructor(
...
private resolver: ComponentFactoryResolver,
private viewContainerRef:ViewContainerRef
) { }
openModal(){
this.cmpRef = this.viewContainerRef.createComponent(
this.resolver.resolveComponentFactory(ModalComponent)
);
this.cmpRef.instance.close.subscribe(e => console.log(e));
}
我得到:
Property 'close' does not exist on type 'Component'.)
但当我做console.log(this.cmpRef.instance);
我可以看到我的组件与所有成员(包括接近)
接近是EventEmitter EN ModalComponent:
@Output() close: EventEmitter<any> = new EventEmitter<any>();
请添加,显示了'close'函数的代码。 –
@GünterZöchbauer编辑我的问题 –