如何在Angular 4+;http拦截器

如何在Angular 4+;http拦截器,angular,dependency-injection,angular-http-interceptors,angular-httpclient,Angular,Dependency Injection,Angular Http Interceptors,Angular Httpclient,我已经实现了HttpInterceptor,用于捕获Angular 5应用程序中的请求错误。我的目标是向服务器发送另一个REST POST请求,从而保存错误日志。看起来是这样的: @Injectable() export class RestErrorHandler implements HttpInterceptor { private readonly ERROR_LOGGER_PATH: string = '/logger/errorEntry'; constructor(privat

我已经实现了HttpInterceptor,用于捕获Angular 5应用程序中的请求错误。我的目标是向服务器发送另一个REST POST请求,从而保存错误日志。看起来是这样的:

@Injectable()
export class RestErrorHandler implements HttpInterceptor {

private readonly ERROR_LOGGER_PATH: string = '/logger/errorEntry';

constructor(private http: HttpClient, private restUtils: RestApiUtilsService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).catch((errorReponse: HttpErrorResponse) => {
        return this.handleRestError(errorReponse, req, next);
    });
}

public handleRestError(error: HttpErrorResponse, req: HttpRequest<any>, next: HttpHandler): ErrorObservable {
    let errorMessage = this.prepareErrorMessage(error);
    let entry = this.prepareEntry(errorMessage);
    this.http.post(this.restUtils.getUrl(this.ERROR_LOGGER_PATH), JSON.stringify(entry), this.restUtils.jsonResponseRequestOptions()).subscribe();
    return Observable.throw(error);
}
}

export const ErrorInterceptorProvider = {
provide: HTTP_INTERCEPTORS,
useClass: RestErrorHandler,
multi: true,
};

如果我创建了另一个@Injectable()服务,通过HttpClient调用RESTAPI,并将其注入RestErrorHandler中,则不会出现错误。为什么会这样?

只需等待下一个角度释放,或应用以下所述的解决方法:

目前这是 只能通过注入喷油器并使用它来解决 HttpClient在请求时


以下是我的最终工作解决方案:

@Injectable()
export class RestErrorHandler implements HttpInterceptor {

private readonly ERROR_LOGGER_PATH: string = '/logger/errorEntry';

constructor(private injector: Injector, private restUtils: RestApiUtilsService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).catch((errorReponse: HttpErrorResponse) => {
        return this.handleRestError(errorReponse, req, next);
    });
}

public handleRestError(error: HttpErrorResponse, req: HttpRequest<any>, next: HttpHandler): ErrorObservable {
    let errorMessage = this.prepareErrorMessage(error);
    let entry = this.prepareEntry(errorMessage);
    if (!this.http) this.http = this.injector.get(HttpClient);
    this.http.post(this.restUtils.getUrl(this.ERROR_LOGGER_PATH), JSON.stringify(entry), this.restUtils.jsonResponseRequestOptions()).subscribe();
    return Observable.throw(error);
}
}
@Injectable()
导出类RestErrorHandler实现HttpInterceptor{
私有只读错误\记录器\路径:字符串='/LOGGER/errorEntry';
构造函数(私有注入器:注入器,私有restUtils:RestApiUtilsService){}
截取(req:HttpRequest,next:HttpHandler):可观察{
返回next.handle(req).catch((ErrorResponse:HttpErrorResponse)=>{
返回此.handleRestError(ErrorResponse,req,next);
});
}
public handleRestError(错误:HttpErrorResponse,请求:HttpRequest,下一步:HttpHandler):ErrorObservable{
让errorMessage=this.prepareErrorMessage(错误);
让entry=this.prepareEntry(errorMessage);
如果(!this.http)this.http=this.injector.get(HttpClient);
this.http.post(this.restUtils.getUrl(this.ERROR\u LOGGER\u PATH)、JSON.stringify(entry)、this.restUtils.jsonresponseRequestStoptions()).subscribe();
返回可观察抛出(错误);
}
}

谢谢。如果有人想知道如何做的细节:看看我下面的答案。
@Injectable()
export class RestErrorHandler implements HttpInterceptor {

private readonly ERROR_LOGGER_PATH: string = '/logger/errorEntry';

constructor(private injector: Injector, private restUtils: RestApiUtilsService) {}

intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(req).catch((errorReponse: HttpErrorResponse) => {
        return this.handleRestError(errorReponse, req, next);
    });
}

public handleRestError(error: HttpErrorResponse, req: HttpRequest<any>, next: HttpHandler): ErrorObservable {
    let errorMessage = this.prepareErrorMessage(error);
    let entry = this.prepareEntry(errorMessage);
    if (!this.http) this.http = this.injector.get(HttpClient);
    this.http.post(this.restUtils.getUrl(this.ERROR_LOGGER_PATH), JSON.stringify(entry), this.restUtils.jsonResponseRequestOptions()).subscribe();
    return Observable.throw(error);
}
}