ngOnInit() - Why use it?

ngOnInit() is always invoked right after a component is constructed. It is also the best place to initialize property values for your script.

ngOnInit() - How to use it?

To use ngOnInit() is very simple, first you must import it with your @angular/core like this to use it.

import { Component, OnInit } from '@angular/core';

then inside the component class you can delcare the function like this for example:

  ngOnInit(): void {
    this.getHeroes();
  }