Я создаю новое имя перехватчика HttpInterceptor
.Вот пример кода:
import { Injectable } from '@angular/core';
import {
HttpRequest,
HttpHandler,
HttpEvent,
HttpInterceptor,
HttpResponse,
HttpErrorResponse,
} from '@angular/common/http';
import { Observable } from 'rxjs';
import { ErrorHandler } from './error_handler';
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
constructor(
public errorHandler: ErrorHandler,
) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => { }, (err: any) => {
if (err instanceof HttpErrorResponse) {
this.errorHandler.handleError(err);
}
});
}
}
Я добавил провайдеров в app.module.ts
providers: [
{
provide: HTTP_INTERCEPTORS,
useClass: RequestInterceptor,
multi: true,
}
]
Я получил эту ошибку.
Ошибка:StaticInjectorError (AppModule) [InjectionToken HTTP_INTERCEPTORS -> ErrorHandler]: StaticInjectorError (Платформа: ядро) [InjectionToken HTTP_INTERCEPTORS -> ErrorHandler]: NullInjectorError: Нет поставщика для ErrorHandler!
1012 *1012* *1012* * 1012я.