Angular:HttpRequest.clone() - params 和 setParams 有什么区别?

标签 angular typescript http httprequest

我有一个拦截器:

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

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

对于 HttpRequest 的 Angular 网站文档没有任何见解。类(class)。它表明他们在那里,但没有任何信息。

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, });
}

关于Angular:HttpRequest.clone() - params 和 setParams 有什么区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58124804/

相关文章:

angular - 我可以创建一个在特定条件下不呈现的 Angular 组件吗?

angular - 如何在 Angular 2 Material 设计的 md-select 组件上设置默认值?

typescript - *ngFor 在 angular2 中运行无限循环

angular - 如何保护 .net 核心 API 项目

TypeScript 在尝试导入时无法定位模块(vue 文件)

TypeScript 1.5 模块系统 - 导出为单例

http - Mule 中的接收器线程配置文件 HTTP 配置

java - javax.faces.webapp.FacesServlet 如何通过 HTTP 工作?

php - PHP curl 到 HTTPS 安全吗?

typescript - Redux 工具包 + typescript : extraReducers