angular的拦截器与retry
2019-07-19 本文已影响0人
BadEvent
引用中文文档里一句话
image.png

这就引出了一个问题,如果我们有a、b两个拦截器,如果ab两个拦截器已经对错误做出了处理的话,retry会失效,所以我们要写一个拦截器c,放在ab后面,以便让响应时的处理为c->b->a
import { Injectable } from "@angular/core";
import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { retry } from 'rxjs/operators';
@Injectable()
export class RetryInterceptor implements HttpInterceptor{
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(req)
.pipe(
retry(3)
)
}
}