angular - ionic 4 : "Loading Controller" dismiss() is called before present() which will keep spinner without dismissing

标签 angular ionic-framework angular6 loading ionic4

我使用“Ionic Loading Controller”来显示微调器,直到检索到数据,然后调用“dismiss()”将其关闭。 它工作正常,但有时当应用程序已经有数据时,“dismiss()”在“create()”和“present()”完成之前被调用,这将保持微调器不被解雇...

我尝试调用“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();
}

最佳答案

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

我使用 bool 变量“isLoading”在调用 dismiss() 时更改为 false。 在 present() 完成后如果“isLoading”=== false(意味着 dismiss() 已经被调用)那么它会立即关闭。

此外,我在服务中编写了代码,因此不必在每个页面中都重新编写。

加载.服务.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'));
  }
}

然后只需从页面调用 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();
    }
  );

关于angular - ionic 4 : "Loading Controller" dismiss() is called before present() which will keep spinner without dismissing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52574448/

相关文章:

angular - 来自不同脚本的多个 Angular 元素

javascript - Angular2 和 Turbolinks

javascript - Data Bind RouterLink (Angular 2) - 获得插值 ({{}}),其中第 3 列预期有表达式

ios ui 与 android 和浏览器 ionic 不同

node.js - 使用命令提示符启动 ionic 时无法验证第一个证书

unit-testing - 我需要帮助来生成对 angular6 中可观察到的 fromEvent 的测试

javascript - 当我们使用 css dropdown 作为共享组件时如何设置和获取表单值

javascript - Angular - 使用 ngFor 创建 Accordion 菜单。需要自动关闭并在单击时打开另一个

javascript - 拼接不适用于 Angularjs

angular6 - 如何通过简单的 http 调用使用 @ngrx/effects?