I am working with ionic 5, and trying to use ion LoadingController with resolvers.
At first, the problem I had was that loadingController.dismiss()
was called before loadingController.create()
finished, so I followed the instructions here:
Ionic 4: "Loading Controller" dismiss() is called before present() which will keep spinner without dismissing.
So, I created a Service to show and dismiss the loader like this:
export class LoaderService {
isLoading = false;
constructor(public loadingController: LoadingController) { }
async present() {
this.isLoading = true;
return await this.loadingController.create().then(a => {
a.present().then(() => {
if (!this.isLoading) {
a.dismiss();
}
});
});
}
async dismiss() {
if (this.isLoading) {
this.isLoading = false;
return await this.loadingController.dismiss();
}
return null;
}
}
And I call it in the app.component
constructor(
private platform: Platform, private loaderService: LoaderService,
private splashScreen: SplashScreen,
private statusBar: StatusBar,
private languageService: LanguageService,
private appDataService: AppDataService,
private popOverCtrl: PopoverController,
private auth: AuthService, private router: Router
) {
this.router.events.subscribe((event: Event) => {
switch(true){
case event instanceof NavigationStart: {
this.loaderService.present();
break;
}
case event instanceof NavigationEnd:
case event instanceof NavigationCancel:
case event instanceof NavigationError: {
this.loaderService.dismiss();
break;
}
default: {
break;
}
}
})
this.initializeApp();
}
But I am getting the following error: