and please do not call asynchronous stuff like requests in the constructor functionā¦ and please do not send requests in components --> use a service for those things
I respect your opinion. Why do you say this? If a component needs to kick off asynchronous tasks in order to prepare itself to display properly, why isnāt the constructor a good place to do that?
yeah, out of the good āoldā oop programming a constructor is called to create an instance
let myInstance = new MyClass();
so in the following code --> you want to be sure (synchronous) that the instanciation of something works and is valid.
like
let myInstance = new MyClass();
myInstance.doSomething();
Now in some mysterious ways the instanciation could fail --> how to check this? --> right try - catch
let myInstance;
try {
myInstance = new MyClass();
} catch(e) {
// stop here
}
myInstance.doSomething();
Now you are doing some asyncronous stuff in the constructor function of myClass and in this asynchronous part an error happens or your whole instanciation is based on the result of something asynchronous - you have no chance to make this save or to be sure everything works like expected.
Even in Angular a component or in ionic Page is simply a class and is instanciated via the dependency Injector.
So you should use the lifecycle hooks like ngOnInit to do initially work. Because instanciation has finished, the @Input bindings have their initial value. Everything is ready to start working.
Fair point. I have been bit by this when using reactive forms, however. Perhaps ngOnInit is good enough, but I have not yet found an Ionic lifecycle event that is early enough to properly initialize FormGroups using the FormBuilder.