我有一个angular 6应用程序,配置为在失败时重试http请求3次.
有没有办法做到这一点?
我在RxJs文档中找不到任何答案.
谢谢你的帮助
这是我的代码:
@Injectable()
export class RetryInterceptor implements HttpInterceptor {
constructor( public spinnerService: NgxSpinnerService ) { }
timer: any;
intercept(req: HttpRequest<any>, next: HttpHandler):
Observable<HttpEvent<any>> {
if(this.timer){
clearTimeout(this.timer);
}
this.timer = setTimeout(() => this.spinnerService.show(), 500);
let handleObs: Observable<HttpEvent<any>> = next.handle(req)
.pipe(
retry(3),
finalize(() =>{
this.spinnerService.hide();
if(this.timer){
clearTimeout(this.timer);
}
}
)
);
return handleObs;
};
}