【发布时间】:2020-05-24 14:13:21
【问题描述】:
我为 http 请求制作了微调拦截器。对于每个显示的 http 请求微调器。
但是有些网页请求比较快,这种情况下spinner会变成网页中的闪烁。
我想为微调器做一些延迟,但我不知道怎么做。
微调器组件
<ng-container *ngIf="loading$ | async">
<mat-progress-bar mode="indeterminate" color='warn'>
</mat-progress-bar>
export class SpinnerComponent implements OnInit {
loading$;
constructor(private spinnerService: SpinnerService) { }
ngOnInit() {
this.loading$ = this.spinnerService.isLoading$
.pipe(
delay(0)
);
}
}
微调服务
export class SpinnerService {
isLoading$ = new Subject<boolean>();
show() {
this.isLoading$.next(true);
}
hide() {
this.isLoading$.next(false);
}
}
微调器拦截器
export class SpinnerInterceptor implements HttpInterceptor {
requestCount = 0;
constructor(private spinnerService: SpinnerService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
this.requestCount++;
this.spinnerService.show();
return next.handle(request)
.pipe(
finalize(() => {
this.requestCount--;
if (this.requestCount === 0) {
this.spinnerService.hide();
}
})
);
}
}
【问题讨论】:
标签: angular rxjs interceptor