0
我想實現一個HttpInterceptor,但不知何故,它根本沒有做任何事情。Angular HttpInterceptor不起作用
定製業務實現HttpInterceptor
:
import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest, HttpResponse, HttpErrorResponse} from '@angular/common/http';
import {AuthenticationService} from "./authentication.service";
import {Observable} from "rxjs/Observable";
@Injectable()
export class HttpInterceptorService implements HttpInterceptor {
constructor(private auth: AuthenticationService) {
}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
console.log("INTERCEPTED!!");
const authHeader = this.auth.getAuthorizationHeader();
const authReq = req.clone({setHeaders: {Authorization: authHeader}});
return next.handle(authReq)
.do((event) => {
if (event instanceof HttpResponse) {
console.log(event);
}
}, (error: HttpErrorResponse) => {
console.log(error);
});
}
}
提供了app.modules.ts
:
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: HttpInterceptorService,
multi: true,
},
AuthenticationService
]
不幸的是,它似乎是無所事事。我錯過了什麼嗎?