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

前端 未结 20 2245
逝去的感伤
逝去的感伤 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:42

    same problem here, and here my solution (ionic 4 and angular 7):

    Started from the acepted solution.

    the present creates the loading one time In the dismiss function, i set isShowing to false only if dimiss returns true

    import { Injectable } from '@angular/core';
    import { LoadingController } from '@ionic/angular';
    
    @Injectable({
      providedIn: 'root'
    })
    export class LoadingService {
    
      isDismissing: boolean;
      isShowing: boolean;
    
      constructor(public loadingController: LoadingController) { 
    
      }
    
      async present() {
        if(this.isShowing){
          return
        }
    
        this.isShowing = true
    
        await this.loadingController.create({spinner: "dots"}).then(re => {
          re.present()
          console.log("LoadingService presented", re.id)
        })
      }
    
      async dismiss() {
        if(this.isShowing){
          await this.loadingController.dismiss().then(res => {
            if(res){
              this.isShowing = false
              console.log("LoadingService dismissed", res);
            }
          })
        }
      }
    }
    

提交回复
热议问题