【问题标题】:Cannot find name 'HttpParamsOptions'. Angular 6找不到名称“HttpParamsOptions”。角 6
【发布时间】:2019-03-26 15:54:20
【问题描述】:

也许有人知道为什么 Angular 不理解 HttpParamsOptions,或者也许有另一种方法可以添加请求头和参数。

`public getJobs(options: {page?: number, size?: number} = {}, database: string): Observable <IJobs[]> {
  const headers = new HttpHeaders().set('Dataset-Id', database);
  const params: any = {
       page: options.page || 0,
       size: options.size || 10
     };
 const httpParams: HttpParamsOptions = { fromObject: params } as HttpParamsOptions;
 const headerwithParams = { params: new HttpParams(httpParams), headers: headers };
   return this.http.get<IJobs[]>('/api/jobs', headerwithParams);
}`

【问题讨论】:

    标签: typescript angular6 request-headers


    【解决方案1】:

    Angular Docs 表示 HttpParams 构造函数可以将 HttpParamsOptions 对象作为其唯一参数。

    但是 Angular 团队忘记为这个类创建任何文档。

    Angular 中有很多缺失的文档。

    【讨论】:

      【解决方案2】:

      如果您有兴趣传递 http 参数,请使用 HttpParams

      对于 HttpClient 获取请求,有多个重载的 get 方法。看到这个HttpClient

      仅传递标头,请参见下文。

      const headers = {};
      headers['Accept'] = 'application/json';
      headers['Content-Type'] = 'application/json';
      this.httpHeaders = new HttpHeaders(headers);
      return this.http.get<IJobs[]>(url_here, {headers: this.httpHeaders })
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-12-15
        • 2017-10-14
        • 2018-06-28
        • 2019-07-06
        • 1970-01-01
        • 2015-07-07
        • 2016-12-27
        • 2022-12-07
        相关资源
        最近更新 更多