【发布时间】:2018-12-19 10:52:17
【问题描述】:
我创建了新的拦截器名称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 没有提供者!
我在 Angular 7 中的代码。请帮助我。
【问题讨论】:
-
如果 Angular 7 是 Rxjs 6,所以“do”被替换为“tap”,更多:你必须改变你的导入 github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/… 并使用管道 github.com/ReactiveX/rxjs/blob/master/docs_app/content/guide/v6/…