import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs';
@Injectable()
export class TokenInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
request = request.clone({
setHeaders: {
Authorization: `${localStorage.token}`
}
});
return next.handle(request);
}
}
// app.module.ts
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: TokenInterceptor,
multi: true
}
]
然后尴尬的一幕出现了,intercepter只在登录前被成功调用了,要用到sessionToken的时候反而罢工了。
google了半天,发现不能有多个HttpClientModule声明。