menu

Questions & Answers

ng generate component does not create ngOnInit and constructor

I'm following the Angular tour of heroes and when I generate the "heroes" component using "ng generate component heroes" this is my component.ts file:

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

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent {

}

but the tutorial said I should be getting something that looks like this:

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

@Component({
  selector: 'app-heroes',
  templateUrl: './heroes.component.html',
  styleUrls: ['./heroes.component.css']
})
export class HeroesComponent implements OnInit {

  constructor() { }

  ngOnInit(): void {
  }

}

I was expecting to get the constructor and ngOnInit function and also import the OnInit automatically. Heres my version if it helps image

Comments:
2023-01-19 10:42:03
It get removed, as they said "most of the users prefer to add this manually if needed." github.com/angular/angular-cli/commit/…
2023-01-19 10:42:03
@paranaaan Can you add your comment as an answer? I've seen this question pop up a lot more since version 15. It will be helpful to have it in a more visible place. What do you think?
Answers(2) :

Is there any possibility to add it by an option of ng generate component?

Comments:
2023-01-19 10:42:03
I flagged this for conversion to a comment or deletion because it is not an answer. Once you earn enough reputation, you will gain the privilege to add comments in addition to answers. Then you will be able to request clarification, add related links, leave constructive criticism, and address other users directly.

It gets removed, as they said "most of the users prefer to add this manually if needed."

Reference: https://github.com/angular/angular-cli/commit/301b5669a724261d53444d5172334966903078c0