The OnInit lifecycle hook is not found in my created angular component file

703 views Asked by At

When I create a new component file in my angular project by " ng g c 'component name' ", OnInit is not there initially in my component file.

3

There are 3 answers

0
MGX On

It has been removed in Angular 15 by default. Just add it back yourself.

0
Nirmal Kumar On

It has been removed in Angular 15. You can also add it manually

@Component({
 ...
})
export class MyComponent extends OnInit {
  ngOnInit(): void {
    // your init code
  }
}
0
Manoj Kumar On

you can add it manually by yourself

@Component({
 selector: 'app-component',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent extends OnInit {

  ngOnInit(): void { 
  }

}