Http 拦截器不记录任何信息

Http interceptor doesn't log any information

我创建了一个 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);
  }

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

我的 angular 服务如下所示:

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

问题是您需要使用新的 HttpClient (Angular 4.3) 来执行请求(从 @angular/common/http 导入而不是从 [=12 导入的旧版本) =].

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