Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ng generate component does not create ngOnInit and constructor

Tags:

angular

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

like image 436
metro Avatar asked Mar 11 '26 02:03

metro


1 Answers

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

like image 53
paranaaan Avatar answered Mar 13 '26 17:03

paranaaan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!