I am getting an error when I try to call a childComponent method from the Parent
**Child TS. **
export class ChildComponent implements OnInit {
ChildMethod(){
console.log('test');
}
constructor() { }
}
**Parent Ts **
@ViewChild(ChildComponent , {static : false}) child!:ChildComponent ;
CallChild(){
this.child.ChildMethod();
}
when I call CallChild I get this error Cannot read properties of undefined (reading 'ChildMethod') at GpmainComponent.CallChild (gpmain.component.ts:39:16)
Try to declare your Child component in constructor.
in Parent.ts:
constructor(private child: ChildComponent) {}
CallChild() {
this.child.ChildMethod();
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments