Angular V4.3,拦截器不会捕获请求或响应

时间:2017-08-25 14:09:37

标签: javascript angular http typescript interceptor

我正在开发一个具有以下配置的项目。抱歉我对Angular世界很陌生。

"@angular/animations": "^4.3.6",
"@angular/common": "^4.3.6",
"@angular/compiler": "^4.3.6",
"@angular/core": "^4.3.6",
"@angular/forms": "^4.3.6",
"@angular/http": "^4.3.6",
"@angular/platform-browser": "^4.3.6",
"@angular/platform-browser-dynamic": "^4.3.6",
"@angular/platform-server": "^4.3.6",
"@angular/router": "^4.3.6",
"@angular/upgrade": "^4.3.6",
"@ng-bootstrap/ng-bootstrap": "^1.0.0-alpha.28",
"angular-font-awesome": "^2.0.2",
"bootstrap": "^4.0.0-alpha.6",
"classlist.js": "^1.1.20150312",
"core-js": "^2.4.1",
"font-awesome": "^4.7.0",
"rxjs": "^5.1.0",
"zone.js": "^0.8.4"

我正在尝试实现4.3版本中发布的内置响应拦截器。 我有拦截服务如下。

import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpHandler, HttpRequest, HttpEvent, HttpResponse }
  from '@angular/common/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/do';
@Injectable()
export class InterceptorService implements HttpInterceptor {
  intercept(
    req: HttpRequest<any>,
    next: HttpHandler
): Observable<HttpEvent<any>> {
    return next.handle(req).do(evt => {
      if (evt instanceof HttpResponse) {
        console.log('---> status:', evt.status);
        console.log('---> filter:', req.params.get('filter'));
      }
    });
}
}

我有共享模块实现,我在其中声明了拦截器。

@NgModule({
  imports: [
    CommonModule,
    NgbModule,
    FormsModule,
    HttpClientModule
  ],
  providers: [
    { provide: HTTP_INTERCEPTORS, useClass: InterceptorService, multi: true }
  ],

出于某种原因,我没有在方法中遇到拦截方法/ console.log。我想知道我错过了什么。任何帮助将不胜感激。仅供参考,我正在使用本教程来处理拦截器。

https://alligator.io/angular/httpclient-interceptors/

0 个答案:

没有答案