无法获得RequestOptions params Angular 2

时间:2017-07-23 20:25:08

标签: angular typescript

我将Http类从@angular/http扩展为使用自定义自定义拦截器来设置令牌标头并在HTTP请求待处理时激活加载器。

我试图拦截GET方法发送的参数:

this.http.get('url', { params: { loader: 'customLoader' } })...

在我的自定义Http服务中:

request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
  options = this.resolveOptions(options);
  this.loaderService.show(this.resolveLoaderParam(options));
  return this.intercept(super.request(this.resolveUrl(url), options));
}

resolveOptions只是设置了身份验证令牌。

问题在于,当我尝试在RequestOptions中提取参数时,在resolveLoaderParam中,它是undefined并且参数已经写在URL中,例如:url?loader=customLoader

更新

private resolveOptions(options?: RequestOptionsArgs): RequestOptionsArgs {
  if (!options) {
    options = new RequestOptions();
  }

  if (!options.headers) {
    options.headers = new Headers();
  }

  options.headers.append('Content-Type', 'application/json');

  const headers = Object.keys(this.headers);

  if (headers.length > 0) {
    headers.forEach((item) => {
      options.headers.append(item, this.headers[item]);
    });
  }

  return options;
}

0 个答案:

没有答案