Ionic 4: “Loading Controller” dismiss() is called before present() which will keep spinner without dismissing

前端 未结 20 2282
逝去的感伤
逝去的感伤 2020-12-08 01:06

I used \"Ionic Loading Controller\" to show a spinner until the data is retrieved then it calls \"dismiss()\" to dismissed it. it works fine, but sometimes when the app alre

20条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 01:35

    My solution for this problem, was to set a status variable. Here it is:

    @Injectable()
        export class LoaderSerive {
        private status: 'pending' | 'dismissed' | 'present' = 'dismissed';
    
        constructor(public loadingCtrl: LoadingController) {}
    
        public show() {
            if (this.status === 'present') {
                this.hide();
            }
    
            this.status = 'pending';
    
            this.loadingCtrl.create({
                id: 'spoon-indicator-1',
                spinner: null,
                message: `
                    
    `, duration: 6000 }) .then((loader) => loader.present()) .then(() => { if (this.status === 'pending') { this.status = 'present'; } else { this.hide(); } }); } public hide() { this.loadingCtrl .dismiss(null, undefined, 'spoon-indicator-1') .catch((err) => Utilities.log('Loader error!', err)) .then(() => this.status = 'dismissed'); } }

提交回复
热议问题