【发布时间】:2018-04-12 21:06:09
【问题描述】:
我正在尝试在我的应用中的每个 http.get 操作上添加一个加载微调器。
这是我的 http.get :
protected get(url: string): any {
//Loading start
this.sessionService.showLoader();
(...)
}
SessionServicecall 我的LoadingService:
constructor(private cookieService: CookieService, private loadingService : LoadingService) {
}
public showLoader(): void {
this.loadingService.show();
}
public hideLoader(): void {
this.loadingService.hide();
}
这是我的服务LoadingService
import { Injectable, OnInit, EventEmitter } from '@angular/core';
@Injectable()
export class LoadingService implements OnInit {
public loadingEvent: EventEmitter<boolean>;
constructor() {
this.loadingEvent = new EventEmitter();
}
ngOnInit() {
this.loadingEvent.emit(false);
}
show() {
this.loadingEvent.emit(true);
}
hide() {
this.loadingEvent.emit(false);
}
}
然后,这是我的主要布局组件的一部分:
showLoadingDiv : boolean;
constructor(public loadingService : LoadingService) {
}
ngOnInit() {
}
ngAfterViewInit(){
this.loadingService.loadingEvent.subscribe((res) => {
this.showLoadingDiv = res;
});
}
最后,在我的 Html 布局模板中:
<div class="loaddiv" *ngIf="showLoadingDiv">
Loading...
</div>
当其他模块快速加载时,我没有错误ExpressionChangedAfterItHasBeenCheckedError,但是如果一个模块需要一点时间(因为数据量很大),我就会遇到这个错误。
我已经看到了这个,但对我不起作用: https://stackoverflow.com/a/38846793/1729211
【问题讨论】:
标签: angular