为什么拦截器类不在Angular 2中工作?

时间:2017-09-20 12:49:06

标签: angular

我有一个拦截器类:

import {Injectable} from '@angular/core';
import {HttpEvent, HttpInterceptor, HttpHandler, HttpRequest} from '@angular/common/http';
import {Observable} from 'rxjs/Observable';

@Injectable()
export class NoopInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log(req);
    return next.handle(req);
  }
}

我将此类添加到主app.module作为提供者:

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

所以,我试图将http响应作为:

console.log(req);

它没有给我任何回报。

Http请求是:

return this.http.post('path', data, options)
      .map(res => (<any>res)._body === '' ? {} : res.json())
      .catch(this.handleError);

我将Http更改为HttpService。它重新要求添加提供者。我补充说:

 providers: [
    HttpService,
    {
      provide: HTTP_INTERCEPTORS,
      useClass: NoopInterceptor,
      multi: true,
    },

现在告诉:ERROR Error: Uncaught (in promise): Error: No provider for ConnectionBackend!

0 个答案:

没有答案