【问题标题】:Typescript http.get Error: No overload matches this callTypescript http.get 错误:没有重载匹配此调用
【发布时间】:2020-06-29 00:56:11
【问题描述】:

我正在尝试从服务器获取文本文件,所以我这样做了:

const httpOptions = {
  headers: new HttpHeaders({
    'Accept': 'text/html',
    'Content-Type': 'text/plain; charset=utf-8'
  }),
  responseType: 'text'
};
this.http.get<any>(url, httpOptions).subscribe(response => {
  const blob = new Blob([response], { type: 'text/csv' });
  const url = window.URL.createObjectURL(blob);
  const anchor = document.createElement('a');
  anchor.download = 'user-permission-auditlog' + endDate.toUTCString() + '.csv';
  anchor.href = url;
  anchor.click();
});

它完全按照我想要的方式工作。然而编译器痛苦地喊道:

错误 TS2769:没有与此调用匹配的重载。 重载 1 的 15, '(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; 观察: "events"; params?: HttpParams | { [param: string ]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }): Observable<...>',给出以下错误。 '{ headers: HttpHeaders; 类型的参数响应类型:字符串; }' 不能分配给类型为 '{ headers?: HttpHeaders | { [标题:字符串]:字符串 |细绳[]; };观察:“事件”;参数?: HttpParams | { [参数:字符串]:字符串 |细绳[]; };报告进度?:布尔值;响应类型?:“json”; withCredentials?:布尔值; }'。 '{ headers: HttpHeaders; 类型中缺少属性'observe'响应类型:字符串; }' 但在类型 '{ headers?: HttpHeaders | { [标题:字符串]:字符串 |细绳[]; };观察:“事件”;参数?: HttpParams | { [参数:字符串]:字符串 |细绳[]; };报告进度?:布尔值;响应类型?:“json”; withCredentials?:布尔值; }'。

它列出了 15 个中的 3 个,他们都抱怨 responseType 应该是 'json' 但作为 responseType 的 'text' 绝对是重载之一:

get(url: string, options: { headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType: "text"; withCredentials?: boolean; }): Observable&lt;string&gt;

https://angular.io/api/common/http/HttpClient#get

我在这里做错了什么?

【问题讨论】:

    标签: angular typescript


    【解决方案1】:

    好的,因此解决方法是将 responseType 更改为 'text' as 'json',然后将 any 的返回类型替换为 string(这是确定使用哪个重载的方式)。

    const httpOptions = {
      headers: new HttpHeaders({
        'Accept': 'text/html',
        'Content-Type': 'text/plain; charset=utf-8'
      }),
      responseType: 'text' as 'json'
    };
    this.http.get<string>(url, httpOptions).subscribe(response => {
      ...
    });
    

    【讨论】:

      【解决方案2】:

      您必须将 httpOptions 转换为 Object。我遇到了同样的问题,它对我来说非常有效。

       const httpOptions : Object = {
        headers: new HttpHeaders({
          'Accept': 'text/html',
          'Content-Type': 'text/plain; charset=utf-8'
        }),
        responseType: 'text'
      };
      

      【讨论】:

      • 选角,是的,宝贝!这是一个!谢谢。
      【解决方案3】:

      根据GitHub,它应该被修复为:

      接受 responseType:'text' 的 get 版本不是通用的,因为您已经声明响应将是一个字符串。

      所以而不是:

      return this.http.get<string>(this.url, {responseType: 'text'});
      

      只使用非通用的:

      return this.http.get(this.url, {responseType: 'text'});
      

      请参阅 SO 回答 here

      【讨论】:

        猜你喜欢
        • 2022-12-07
        • 2020-02-15
        • 2020-11-28
        • 2021-12-31
        • 2020-03-28
        • 1970-01-01
        • 1970-01-01
        • 2021-11-15
        • 2023-01-04
        相关资源
        最近更新 更多