Home > Software engineering >  ng generate component does not create ngOnInit and constructor
ng generate component does not create ngOnInit and constructor

Time:12-22

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

CodePudding user response:

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

  • Related