在重写的http方法angular中执行http请求

时间:2017-04-30 03:00:23

标签: angular angular2-http

您好我想在Http角对象的重写请求方法中执行http.get(...)。

@Injectable()
export class CustomHttp extends Http {
  constructor(backend: XHRBackend, options: RequestOptions, ) {
    super(backend, options);
  }

  request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
    //some logics
    if(//some conditions)
         super.get('url');
    return super.request(url, options).catch(this.catchError(this)); 
  }
}

当前行为是将http请求跳过到重写的request()

我使用角度4。

1 个答案:

答案 0 :(得分:0)

就像在https://github.com/angular/angular/blob/5293794316cc1b0f57d5d88b3fefdf6ae29d0d97/packages/http/src/http.ts#L113

中完成的一样
return backend.createConnection(request).response;

private createRequest(backend: ConnectionBackend, request: Request): Observable<Response> {
  return backend.createConnection(request).response;
}

...

  let responseObservable: any;
  if(//some conditions)
    responseObservable = httpRequest(this._backend,
          new Request(mergeOptions(this._defaultOptions, options, RequestMethod.Get, <string>url)));
  }
  return responseObservable;