First of all, if you are using Ionic, I would suggest checking ionic lifecycle events. More information can be found here.
To answer your question, if you want to use OnInit, you should import the OnInit and add implements OnInit
to the class declaration (as you have already done). If you fail to do so, the Typescript type checks would give you an error message (since this is typescript). It's a good practice thing, and validators won't shout about invalid code.
If you don't want to add implements OnInit
, you could use event ionViewDidLoad
or any other that would fit best for your need.
So to summarize regarding the best practice:
- Use one of the Ionic events, if you are developing Ionic app and creating a page.
- Use ngOnInit and do
implements OnInit
next to class declaration if you are developing an Angular app OR if you need to do something on initialization of a component within Ionic.