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

前端 未结 20 2287
逝去的感伤
逝去的感伤 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条回答
  •  旧时难觅i
    2020-12-08 01:38

    This way it also resolved concurrent API call loader dismiss issue fix. You can call those functions to form the interceptor too. There is no fix duration, because if any call needs much time loader will continue. But if anyone gives duration then if that API won't stop by this time loader will stop

    import { Injectable } from '@angular/core';
    import { LoadingController } from '@ionic/angular';
    
    @Injectable({
      providedIn: 'root'
    })
    export class LoadingService {
      isLoading = false;
      loaderCounter = 0;
      loading: HTMLIonLoadingElement;
    
      constructor(public loadingController: LoadingController) {}
    
      async present() {
        this.loaderCounter = this.loaderCounter + 1;
    
        if (this.loaderCounter === 1) {
          this.isLoading = true;
          const { loadingDuration, loadingMessage = loadingDefaultOptions.loadingMessage, loadingCssClass } = options;
          this.loading = await this.loadingController.create({
            duration: loadingDuration,
            message: loadingMessage,
            cssClass: loadingCssClass
          });
          await this.loading.present();
        }
      }
    
      async dismiss() {
        this.loaderCounter = this.loaderCounter - 1;
        if (this.loaderCounter === 0) {
            this.isLoading = false;
            await this.loading.dismiss();
        }
      }
    }
    

提交回复
热议问题