多个加载指示器显示在彼此上方
Multiple loading indicators shown above each other
我有一个用 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;
}
}
在加载程序进入之前,您不会更新加载状态。如果输入是异步的,则可能会出现竞争条件:
show()
被称为
- 加载程序已创建
show()
又被别的东西调用了
- 创建了第二个加载器
- 第一个loader进入,更新状态
我有一个用 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;
}
}
在加载程序进入之前,您不会更新加载状态。如果输入是异步的,则可能会出现竞争条件:
show()
被称为- 加载程序已创建
show()
又被别的东西调用了- 创建了第二个加载器
- 第一个loader进入,更新状态