我正在尝试将
为了进行JWT刷新,我扩展了
这是我目前拥有的拦截器:
问题在于我无法注入
Http
更新为更新的HttpClient
。为了进行JWT刷新,我扩展了
Http
类并重写了request()
方法(https://stackoverflow.com/a/45750985/2735398)。
现在我想用拦截器做同样的事情。这是我目前拥有的拦截器:
export class JwtRefreshInterceptor implements HttpInterceptor {
public constructor(
private httpClient: HttpClient,
) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).catch((error: HttpErrorResponse) => {
if (error.status === 401) {
return this.httpClient.post(environment.base_uri + 'auth/refresh', {}).flatMap((response) => {
// get token from response.
// put token in localstorage.
// add token to the request.
// Do the request again with the new token.
return next.handle(request);
});
}
return Observable.throw(error);
});
}
}
问题在于我无法注入
HttpClient
,因为我会得到一个错误:Provider parse errors:
Cannot instantiate cyclic dependency! InjectionToken_HTTP_INTERCEPTORS ("[ERROR ->]"): in NgModule AppModule in ./AppModule@-1:-1
通过扩展 Http
,我只需要调用this.post()
,因为我是在Http
实例本身中工作。但使用拦截器时,这样做是不行的。
如何在拦截器内发起HTTP请求?