ANGLING:HttpRequest.Clone()-参数和setParams之间有什么区别? [英] Angular: HttpRequest.clone() - What is the difference between params and setParams?

查看:8
本文介绍了ANGLING:HttpRequest.Clone()-参数和setParams之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个拦截器:

intercept(request, next) {
  const _id = 1
  const _token = "foo"
  return next.handle(request.clone({ 
    setParams: {
     user_id: _id,
     user_token: _token
    }
  });
}

我注意到,不是setParams,而是params字段?

HttpRequest类的角度网站文档上没有任何见解。它显示他们在那里,但没有任何信息。

params是硬覆盖,setParams是将额外的Key->Value对追加到请求的方法吗?

推荐答案

HttpRequest.clone()方法提供支持传递update对象的重载:

clone(update: { headers?: HttpHeaders; reportProgress?: boolean; 
  params?: HttpParams; responseType?: "arraybuffer" | "blob" | "text" | "json"; 
  withCredentials?: boolean; body?: T; method?: string; url?: string; 
  setHeaders?: { ...; }; setParams?: { ...; }; }): HttpRequest<T>
  • params-提供使用HttpParams实例分配HTTP参数的选项,覆盖进程中的现有参数。
  • setParams-提供使用对象文字追加HTTP参数的选项,其中键是要设置的参数的名称。

Source code for clone() method

clone(update: {
  headers?: HttpHeaders,
  reportProgress?: boolean,
  params?: HttpParams,
  responseType?: 'arraybuffer'|'blob'|'json'|'text',
  withCredentials?: boolean,
  body?: any|null,
  method?: string,
  url?: string,
  setHeaders?: {[name: string]: string | string[]},
  setParams?: {[param: string]: string};
} = {}): HttpRequest<any> {
  // For method, url, and responseType, take the current value unless
  // it is overridden in the update hash.
  const method = update.method || this.method;
  const url = update.url || this.url;
  const responseType = update.responseType || this.responseType;

  // The body is somewhat special - a `null` value in update.body means
  // whatever current body is present is being overridden with an empty
  // body, whereas an `undefined` value in update.body implies no
  // override.
  const body = (update.body !== undefined) ? update.body : this.body;

  // Carefully handle the boolean options to differentiate between
  // `false` and `undefined` in the update args.
  const withCredentials =
      (update.withCredentials !== undefined) ? update.withCredentials : this.withCredentials;
  const reportProgress =
      (update.reportProgress !== undefined) ? update.reportProgress : this.reportProgress;

  // Headers and params may be appended to if `setHeaders` or
  // `setParams` are used.
  let headers = update.headers || this.headers;
  let params = update.params || this.params;

  // Check whether the caller has asked to add headers.
  if (update.setHeaders !== undefined) {
    // Set every requested header.
    headers = Object.keys(update.setHeaders)
        .reduce((headers, name) => headers.set(name, update.setHeaders ![name]), headers);
  }

  // Check whether the caller has asked to set params.
  if (update.setParams) {
    // Set every requested param.
    params = Object.keys(update.setParams)
        .reduce((params, param) => params.set(param, update.setParams ![param]), params);
  }

  // Finally, construct the new HttpRequest using the pieces from above.
  return new HttpRequest(
      method, url, body, { params, headers, reportProgress, responseType, withCredentials, });
}

这篇关于ANGLING:HttpRequest.Clone()-参数和setParams之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆