I’m building an app in which I have several pages which I can navigate to (always trough routing, as suggested on new ionic 4 rather than with navController).
My initial idea was to handle this from my AppComponent which is the bootstrap component which is always loaded and present/dismiss the loading spinner on routing navigations by listening to Angular Route Events. I’ve found out that ionic lifecycle hooks are triggered always after the NavigationEnd event of Angular router, so it is not possible to handle this from these events.
I’ve also found out that the option for a new loading spinner to be presented dismissOnPageChange
is not available anymore, which initially seemed really handy since we would expect the loader to automatically hide once the new page that has been navigated to is shown.
Then I’ve tried to provide a service provided in root which all page components could trigger methods either to show a loading spinner, or hide a loading spinner depending on the lifecycle hook method. Been playing around with ionViewWillEnter
/ ionViewWillLeave
and ionViewDidEnter
/ionViewDidLeave
. The problem with this is that the order of the methods triggered are not always the expected ones, if I’m navigating from page1 to page2 I would expect always for the methods ionViewWillLeave
/ionViewDidLeave
from page1 to trigger first and then the methods ionViewWillEnter
/ionViewDidEnter
from page2 afterwards. This doesn’t happen so the behaviour is not the expected one.
Currently I’m mixing the approaches, I’am listening for the NavigationEnd event at the AppComponent for showing the loading spinner. And I’m hiding it on the different pages when the methods ionViewWillEnter
/ionViewDidEnter
are trigger. Still the result seems buggy.
The best current approach I’ve found so far is using something like:
async navigate() {
const loading = await this.loadingController.create({});
await loading.present();
await this.router.navigate(['/your-route']);
await loading.dimiss();
}
Based on this post Ionic 4 dismissOnPageChange but instead of doing it with the navController, doing it with the router itself. The problem with this is that by doing so, I’ve always have to navigate to pages programmatically, and this is not always desired. I believe the framework should provide a more easier ‘out-of-the-box’ approach for achieving this kind of behavior.
Does anyone has an idea about how this can be done?
Thanks in advance