I'm starting out in angular2, and I'm wondering about this code
export class HeroesComponent implements OnInit {
// Version 1
myHero: Hero = new Hero('Superman', 'Clark Kent');
// Version 2, 3
// myHero: Hero;
constructor() {
// Version 2
// this.myHero = new Hero('Superman', 'Clark Kent');
}
ngOnInit() {
// Version 3
// this.myHero = new Hero('Superman', 'Clark Kent');
}
}
Right now I've got myHero initialized at the top, but I'm wondering, what goes at the top, what should be inside the constructor and what goes inside ngOnInit?
Because as far as I know, if it's at the top, it's executed straight away, same with the constructor, and ngOnInit?
So what's the difference, and what's correct?
Thank you