对于我的应用程序,我创建了以下HttpInterceptor
. 有没有办法从这里向请求订阅者返回响应的更改版本?
import { Injectable } from '@angular/core';
import { HttpRequest, HttpResponse, HttpErrorResponse, HttpHandler, HttpEvent, HttpInterceptor } from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import { Router } from '@angular/router';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(
private router: Router
) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// any way to alter response that gets sent to the request subscriber?
}
}, (error: any) => {
if (error instanceof HttpErrorResponse) {
if (error.status === 401 || error.status === 403) {
console.log('The authentication session has expired or the user is not authorised. Redirecting to login page.');
this.router.navigate(['/login']);
}
}
});
}
}
谢谢你。