I am struggling to call a child component method from my parent component and pass an object. When I try to do so, I encounter the following error: Cannot read property 'RunMe' of undefined
What am I missing?
Child component:
runMe = (item) => {
this.cdr.detectChanges();
if (item.hidden) {
this.showErrorMsgEvent.emit();
} else {
this.highLightEvent.emit(item);
}
}
Parent component:
@ViewChild(childComponent, { static: true }) childComponent;
ListenIframeEvents(){
window.addEventListener("message", this.displayMessage, false)
}
displayMessage (item) {
this.childComponent.RunMe(item)
}
<child-component #childComponent></child-component>
After researching online, I found suggestions to add the # selector but it did not work for me.
Just to clarify, when I run this.childComponent.RunMe() function under the ngOnInit(), it works perfectly fine. I am puzzled as to what I am doing wrong.