问题描述
在我的应用程序中,我有一个微调器,它在 GET
函数完成后停止。对于一个屏幕,即使 GET
函数在网络中看起来已经完成,微调器也不会隐藏。计数器永远不会变为 0。可能是什么问题?
微调服务:
private _spinner: FuseSpinnerComponent;
private _callCount: number = 0;
public hide(): void {
this._callCount--;
if (this._spinner && this._callCount <= 0) {
this._spinner.Hide();
}
}
HTTP 服务:
get(url: string,contentType?: ContentTypes): Observable<any> {
this._spinner.show();
return this._http.get(this.baseUri + url,{ headers: this.getHeader(contentType) }).pipe(
catchError((err: any) => this.handleError(err)),finalize(() => {
this._spinner.hide();
}),);
}
屏幕:
if (state.url.indexOf("/admin/contact-list") >= 0) {
return new Promise((resolve,reject) => {
Promise.all([
this.getContactList(),this.getGeoTypeList(),this.getSectorTypeList(),]).then(
([results]) => {
resolve([]);
},reject
);
});
}
getContactList() {
return new Promise((resolve,reject) => {
this._http
.get("Crm/GetContactList/")
.subscribe((response: IContact[]) => {
this.contactList = response;
this.onContactListChanged.next(
this.contactList
);
resolve(this.contactList);
},reject);
});
}
getGeoTypeList(): Observable<IGeoType[]> {
return this._http.get("Parameter/GetGeoTypeList");
}
getSectorTypeList(): Observable<IGeoType[]> {
return this._http.get("Parameter/GetSectorTypeList");
}
解决方法
Http 客户端请求很冷,您必须订阅它们才能获取数据。
get(url: string,contentType?: ContentTypes): Observable<any> {
this._spinner.show();
return this._http.get(this.baseUri + url,{ headers:
this.getHeader(contentType) }).pipe(
catchError((err: any) => this.handleError(err)),}).subscribe( _ => this._spinner.hide())
);
}