Http拦截器没有记录任何信息

时间:2017-10-11 14:19:07

标签: angular http interceptor angular-http-interceptors

我创建了一个CustomHttpInterceptor,现在我只想记录一些信息,例如:

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

当然我在providers app.module注册了它,但没有记录任何内容,我向服务器发出了一些请求。

我的angular服务如下:

import {Http} from "@angular/http";
 getAllWithPaging(params: HttpParams){
    return this.http.get(this.url + '/withPaging?' + params.toString());
  }

1 个答案:

答案 0 :(得分:1)

问题是您需要使用新的HttpClient(Angular 4.3)来执行请求(从@angular/common/http导入而不是从@angular/http导入的旧请求。

import { HttpClient, HttpParams } from '@angular/common/http';

@Injectable()
export class MyService {
  constructor(private httpClient: HttpClient) {}

  getAllWithPaging(params: HttpParams) {
    return this.httpClient.get(this.url + '/withPaging?' + params.toString());
  }
}

有关详细指南,请参阅official documentation