【发布时间】:2021-07-21 18:44:47
【问题描述】:
我正在尝试覆盖 NgxSpinner 的显示和隐藏方法,以防止在进程持续时间太短时出现短时显示微调器。
这是我在扩展 NgxSpinnerService 的类中的代码
import { Injectable } from '@angular/core';
import { NgxSpinnerService, Spinner } from 'ngx-spinner';
@Injectable({
providedIn: 'root'
})
export class SpinnerService extends NgxSpinnerService {
constructor() { super(); }
private onDisplay: boolean = false;
show (name?: string, spinner?: Spinner): Promise<unknown> {
this.onDisplay = true;
setTimeout(() => {
console.log(`showing ${name} | onDisplay ${this.onDisplay}`);
if (this.onDisplay) return super.show(name, spinner);
}, 300);
return null;
}
hide (name?: string, debounce?: number): Promise<unknown> {
this.onDisplay = false;
return super.hide(name, debounce);
}
}
这是我从中调用方法的组件的片段
constructor(
private _graphService: GraphsService,
private _medicinesService: MedicinesServices,
private _notification: NotificationService,
private fb: FormBuilder,
private spinner: SpinnerService
) { }
ngOnInit (): void {
this.init();
}
private init () {
this.spinner.show('component');
this._medicinesService.getAllGrupedBy().subscribe(
(data) => {
...
this.loadData();
},
(error) => {
this._notification.showErrorToast(error.errorCode);
this.spinner.hide('component');
}
);
}
private loadData (): void {
this.spinner.show('component');
if (!this.selectedOption) this.selectedOption = this.options[0];
this.getData().subscribe(
(data) => {
...
this.spinner.hide('component');
},
(error) => {
this.spinner.hide('component');
this._notification.showErrorToast(error.errorCode);
}
);
}
app.component.html 中的 HTML
<div [ngClass]="{ 'loading-container': !isMobileScreen }">
<ngx-spinner [name]="spinnersConf.component.name" [fullScreen]="false" [bdColor]="spinnersConf.component.bgColor">
<p style="font-size: 20px; color: white">{{ spinnersConf.component.text | translate }}</p>
</ngx-spinner>
</div>
控制台出现在开发者工具窗口中,但微调器不会显示。但是,如果我从 NgxSpinnerService 调用 show 方法,它会出现没有问题。
我的服务有任何错误吗?
【问题讨论】:
-
你好@Lorena Sineiro 什么时候显示微调器任何场景?请告诉我,我可以建议你
-
你好@KiranMistry 我更新了问题,谢谢!
-
好的所以@Lorena Sineriro你试图在API调用或来自API的一些数据正常时显示微调器,当数据完全到达前端时你试图隐藏微调器所以我建议你不要采用这种方式,因为如果您更喜欢这种方式,则每次调用 API 时都必须添加
show()和hide()。因此,如果您有任何问题,我会发布我的答案,然后告诉我我会帮助您;) -
如果您有任何问题,请查看我的回答,然后发表评论,我会帮助您
标签: angular ngx-spinner