這裏是我的代碼不能在動態創建的組件訪問成員屬性
模板:
<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編輯我的問題 –