Reputation: 1881
I am making an ionic 3 app and wanted to add a loading spinner on every navigation. So I created a loadingService and added it to my navigationService in order to handle all navigations automatically.
In order to start and stop loader I used:
export class LoaderService{
loading: Loading;
constructor(public loadingCtrl: LoadingController ){
this.loading = this.loadingCtrl.create({
spinner: 'crescent'
})
}
startLoader(){
this.loading.present();
}
stopLoader(){
this.loading.dismiss();
}
}
After the first navigation I get errors
ERROR Error: Uncaught (in promise): inserted view was already destroyed
ERROR Error: Uncaught (in promise): removeView was not found
Upvotes: 1
Views: 1741
Reputation: 1881
The issue was that with this.loading.dismiss()
loader instance are not dismissed correctly, so what you have to do before starting a new loader is:
this.loading.dismissAll();
this.loading = null;
So I altered my service like this (this can be better but you'll get the idea).
export class LoaderService{
loading: Loading;
constructor(public loadingCtrl: LoadingController ){
}
startLoader(){
this.loader();
return this.loading.present();
}
stopLoader(){
this.loading.dismissAll();
this.loading = null;
}
private loader(){
if(this.loading && this.loading.instance){
this.stopLoader();
}
this.loading = this.loadingCtrl.create({
spinner: 'crescent',
dismissOnPageChange: true,
})
}
}
Upvotes: 3