Ionic 4:"加载控制器"dismiss()在present()之前调用,它将保持微调器而不会被忽略

ram*_*hin 16 loading ionic-framework angular ionic4 angular6

我使用"离子加载控制器"来显示一个微调器,直到检索到数据然后它调用"dismiss()"来解除它.它工作正常,但有时当应用程序已经拥有数据时,在"create()"和"present()"完成之前调用"dismiss()",这将保持微调器而不会消失...

我试图在"loadingController.present().then()"中调用数据,但这导致数据变慢...

这是一个错误吗?如何解决这个问题?

我的代码示例:

customer: any;

constructor(public loadingController: LoadingController, private customerService: CustomerService)

ngOnInit() {
  this.presentLoading().then(a => consloe.log('presented'));
  this.customerService.getCustomer('1')
  .subscribe(customer => {
    this.customer = customer;
    this.loadingController.dismiss().then(a => console.log('dismissed'));
  }
}

async presentLoading() {
  const loading = await this.loadingController.create({
    message: 'wait. . .',
    duration: 5000
  });
  return await loading.present();
}
Run Code Online (Sandbox Code Playgroud)

ram*_*hin 53

这就是我解决问题的方法..

调用dismiss()时,我使用布尔变量"isLoading"更改为false.然后在present()完成后如果isLoading === false(意味着已经调用dismiss())那么它将立即解除.另外,我在服务中编写了代码,所以我不必在每个页面中再次编写代码.

loading.service.ts

import { Injectable } from '@angular/core';
import { LoadingController } from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class LoadingService {

  isLoading = false;

  constructor(public loadingController: LoadingController) { }

  async present() {
    this.isLoading = true;
    return await this.loadingController.create({
      duration: 5000,
    }).then(a => {
      a.present().then(() => {
        console.log('presented');
        if (!this.isLoading) {
          a.dismiss().then(() => console.log('abort presenting'));
        }
      });
    });
  }

  async dismiss() {
    this.isLoading = false;
    return await this.loadingController.dismiss().then(() => console.log('dismissed'));
  }
}
Run Code Online (Sandbox Code Playgroud)

然后只需从页面调用present()和dismiss().

有问题的例子:

customer: any;

constructor(public loading: LoadingService, private customerService: CustomerService)

ngOnInit() {
  this.loading.present();
  this.customerService.getCustomer('1')
  .subscribe(
    customer => {
      this.customer = customer;
      this.loading.dismiss();
    },
    error => {
      console.log(error);
      this.loading.dismiss();
    }
  );
Run Code Online (Sandbox Code Playgroud)

注意:不要忘记将"LoadingService"添加到AppModule的提供者

  • 我喜欢这个效果很好的解决方案。我只是时不时地经历过这个控制台错误:“覆盖不存在”。我解决了这个问题,检查 getTop() 是否返回一些值。 (3认同)

Cod*_*Spy 5

对于离子4检查此解决方案

链接

  import { Component } from '@angular/core';
  import { LoadingController } from '@ionic/angular';

  @Component({
    selector: 'app-home',
    templateUrl: 'home.page.html',
    styleUrls: ['home.page.scss'],
  })
  export class HomePage {

    loaderToShow: any;

    constructor(
      public loadingController: LoadingController
    ) {
    }


    showAutoHideLoader() {
      this.loadingController.create({
        message: 'This Loader Will Auto Hide in 2 Seconds',
        duration: 20000
      }).then((res) => {
        res.present();

        res.onDidDismiss().then((dis) => {
          console.log('Loading dismissed! after 2 Seconds');
        });
      });
    }

    showLoader() {
      this.loaderToShow = this.loadingController.create({
        message: 'This Loader will Not AutoHide'
      }).then((res) => {
        res.present();

        res.onDidDismiss().then((dis) => {
          console.log('Loading dismissed! after 2 Seconds');
        });
      });
      this.hideLoader();
    }

    hideLoader() {
      setTimeout(() => {
        this.loadingController.dismiss();
      }, 4000);
    }

  }
Run Code Online (Sandbox Code Playgroud)


And*_* S. 5

这是我在项目中解决相同问题的方式。我在HTTP拦截器中使用此服务来显示应用程序中所有REST API调用的加载程序。

loading.service.ts

import {Injectable} from '@angular/core';
import {LoadingController} from '@ionic/angular';

@Injectable({
  providedIn: 'root'
})
export class LoadingService {
  constructor(public loadingController: LoadingController) {
  }

  async present(options: object) {
    // Dismiss all pending loaders before creating the new one
    await this.dismiss();

    await this.loadingController
      .create(options)
      .then(res => {
        res.present();
      });
  }

  /**
   * Dismiss all the pending loaders, if any
   */
  async dismiss() {
    while (await this.loadingController.getTop() !== undefined) {
      await this.loadingController.dismiss();
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在原始问题上下文中,可以按以下方式使用:

...
import {LoadingService} from '/path/to/loading.service';
...
customer: any;

constructor(public loadingService: LoadingService, private customerService: CustomerService)

ngOnInit() {
  this.loadingService.present({
    message: 'wait. . .',
    duration: 5000
  });
  this.customerService.getCustomer('1')
  .subscribe(customer => {
    this.customer = customer;
    this.loadingService.dismiss();
  }
}
Run Code Online (Sandbox Code Playgroud)