Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 4 : ngOnInit vs ionViewWillEnter

Which is better to use Angular Lifecycle Hook or Ionic Lifecycle hooks specially for initialization when creating a hybrid app using Ionic 4?

Angular lifecycle hook - ngOnInit

ngOnInit() {
    this.getData();
}

Ionic lifecycle hook - ionViewWillEnter

ionViewWillEnter() {
    this.getData();
}
like image 974
Varun Sukheja Avatar asked Jan 21 '19 17:01

Varun Sukheja


People also ask

What is ngAfterViewChecked?

ngAfterViewChecked()linkA callback method that is invoked immediately after the default change detector has completed one change-check cycle for a component's view.

How do you destroy components in ionic?

Alternatively, You can use ngOnDestroy and ngOnInit of your parent page, but make sure they are called when you suppose them to be called (Ionic stack pages instead of destroying them and calls ngOnDestroy only if that page is on the top of stack and is then removed from the stack, otherwise it uses it's additional ...

What do you mean by ionic life cycle?

Ionic Lifecycle Methods​Fired when the component routing from is about to animate. ionViewDidLeave. Fired when the component routing to has finished animating. The way you access these methods varies based on if you are using class-based components or functional components.


2 Answers

ionViewWillEnter — Fired when entering a page (also if it’s come back from stack)

ngOnInit will not be triggered, if you come back to a page after putting it into a stack

i think better once ionviewwillenter

like image 77
siva kumar Avatar answered Sep 21 '22 12:09

siva kumar


The Ionic 4 migration guide puts it this way:

With V4, we're now able to utilize the typical events provided by Angular. But for certain cases, you might want to have access to the events fired when a component has finished animating during it's route change. In this case, the ionViewWillEnter, ionViewDidEnter, ionViewWillLeave, and ionViewDidLeave have been ported over from V3. Use these events to coordinate actions with Ionic's own animations system.

So the bottom line is to prefer Angular lifecycle hooks like ngOnInit if possible. The only real exception is dealing with Ionics animation system like checking if a component has finished it's entering animation.

like image 21
Phonolog Avatar answered Sep 20 '22 12:09

Phonolog