Call method on another component Angular is giving error

0

Whenever I try to call a method from another Component in the Angular, it returns me an error. Below is my TypeScript code:

Component1:

  metodoAlertar() {
    console.log('bls')
  }

Component2:

  cpmn: Component1Component;
  constructor() { 
  }
  ngOnInit() {
    this.cpmn.metodoAlertar();
  }

I want whenever my Component2 injects, it calls the present method in Component2 that creates a console.log but it is returning me this following error:

    
asked by anonymous 17.12.2018 / 18:30

1 answer

0

If it's a child element you have to sweat the ViewChild to get the reference.

<child-tag #varName></child-tag>

@ViewChild('varName') someElement;

ngAfterViewInit() {
  someElement...
}
    
17.12.2018 / 19:14