Reputation: 10949
I received a warning from TS Lint that I need to implement OnInit interface and provided me with a link to this page : https://angular.io/docs/ts/latest/guide/style-guide.html#!#09-01
What is the difference between onInit
and ngOnInit
. Both worked for me .
Why is ok to use ngOnInit
and not onInit
which is much simple to write?
Upvotes: 4
Views: 3167
Reputation: 3612
Angular always checks for lifecycle hooks, whether your class implements the interface or not.
https://angular.io/docs/ts/latest/guide/lifecycle-hooks.html
So if you have this class:
export class MyComponent {
ngOnInit() { ... }
}
It will work fine (ngOnInit will be called by the framework). However, it is a good idea to implement the interface to ensure the method has been properly implemented.
export class MyComponent implements OnInit {
ngOnInit() { ... }
}
Note that onInit
is NOT a lifecycle hook and will not be called by Angular -- it is simply the name of the interface saying that you implement
ngOnInit
method somewhere in your class.
Upvotes: 7