角度6 HttpInterceptor不触发

时间:2018-11-01 18:29:23

标签: angular angular6

我正在尝试实现我的HttpErrorInterceptor来捕获我的angular 6应用程序中来自REST服务的HTTP错误,但未触发。以下是我的代码,我错过了一些东西/我在做错什么?

export class HttpErrorInterceptor implements HttpInterceptor {
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    return next.handle(request)
        .pipe(
            catchError( (error: HttpErrorResponse) => {
                console.log('interceptor works!');
                let errMsg = '';
                // Client Side Error
                if (error.error instanceof ErrorEvent) {
                    errMsg = `Error: ${error.error.message}`;
                } else {  // Server Side Error
                    errMsg = `Error Code: ${error.status},  Message: ${error.message}`;
                }
                return throwError(errMsg);
            })
        );
}

}

app.module.ts

    providers: [
    , {
        provide: HTTP_INTERCEPTORS, useClass: HttpErrorInterceptor, multi: true
    }
],

1 个答案:

答案 0 :(得分:0)

我刚刚意识到我的一个模块也正在导入HttpClientModule(除了我的app.module.ts。一旦删除它,它就像一个魅力!