Ionic4--loading异步加载,导致无法正确消失

原因:由于异步导致dissmiss()方法先走,然后才执行present()方法

在stackoverflow上找到了比较好的解决办法

public loadingIsOpen: any = false;

 loading加载
  async show() {
    this.loadingIsOpen = true;
    return await this.loadingController.create({
      duration: 7000,
    }).then(a => {
      a.present().then(() => {
        if (!this.loadingIsOpen) {
          a.dismiss()
        }
      });
    });
  }

loading结束
  async hide() {
    if (this.loadingIsOpen == true) {
      this.loadingIsOpen = false;
      return await this.loadingController.dismiss();
    }
  }

 

你可能感兴趣的:(Ionic)