Reputation: 9736
This the code that is used to load a variable with data on the page, using ngOnInit
method:
However, I notice that even if we don't import the OnInit (line 1) and don't write implements OnInit
(line 10), the method is still invoked. What is the best practice?
Upvotes: 0
Views: 4615
Reputation: 187
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:
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.Upvotes: 3