上方显示多个加载指示器

问题描述 投票:0回答:1

我有一个用ionic 3制作的应用程序,在其中创建了一个提供程序来集中访问LoadingController。

我已经按照下面所示的代码实现了提供程序,我认为足以控制应用程序中所有位置的加载指示器。

我不知道如何,但是有时实例化了指示器的多个实例,即使实例化新实例之前使用了[[if(!this.isShowing())验证。

有人可以帮我弄清楚发生了什么吗?预先感谢。

import { Injectable } from '@angular/core'; import { LoadingController, Loading, Platform } from 'ionic-angular'; import { BehaviorSubject } from 'rxjs'; export enum LoadingStatus { SHOWING, DISMISSED, } @Injectable() export class LoadingProvider { private loading: Loading = null; private status: BehaviorSubject<LoadingStatus> = new BehaviorSubject(LoadingStatus.DISMISSED); constructor(private loadingCtrl: LoadingController, private platform: Platform) { this.platform.ready().then(() => { this.status.next(LoadingStatus.DISMISSED); }); } async show(content?: string) { if (!this.isShowing()) { this.create(content); await this.loading.present(); } } async dismiss() { if (this.isShowing()) { await this.loading.dismiss(); this.loading = null; } } private create(content?: string) { this.loading = this.loadingCtrl.create({ content: content ? content : 'Carregando...', showBackdrop: true, enableBackdropDismiss: true, }); this.loading.didEnter.subscribe(() => { if (this.status.getValue() === LoadingStatus.DISMISSED) { this.updateLoadingStatus(LoadingStatus.SHOWING); } }); this.loading.didLeave.subscribe(() => { if (this.status.getValue() === LoadingStatus.SHOWING) { this.updateLoadingStatus(LoadingStatus.DISMISSED); } }); } private async updateLoadingStatus(status: LoadingStatus) { this.status.next(status); } private isShowing(): boolean { return this.status.getValue() === LoadingStatus.SHOWING; } }

javascript ionic-framework rxjs ionic3 angular5
1个回答
0
投票
直到加载程序进入后,您才更新加载状态。如果输入是异步的,则有可能出现竞争状况:

    show()被称为
  1. 已创建加载程序
  2. [show()被其他人再次调用
  3. 创建第二个加载器
  4. 第一个加载程序进入,更新状态
© www.soinside.com 2019 - 2024. All rights reserved.