简体   繁体   中英

Ionic 3 LoadingController removeView was not found

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

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,
        })
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM