angular - HttpClient.get 没有重载匹配这个调用
问题描述
我希望能够下载文件(这里使用 blob)。为此,我需要从嵌入在标题“content-disposition”中的服务器中检索文件名。这是我的功能:
const header = {Authorization: 'Bearer ' + token};
const config = {headers: header, responseType: 'blob' as 'blob', observe: 'response' as 'response'};
return this.http.get<HttpResponse<Blob>>(url, config);
但我得到了错误:
error TS2345: Argument of type '{ headers: { Authorization: string; }; responseType: "blob"; observe: "response"; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'.
Types of property 'observe' are incompatible.
Type '"response"' is not assignable to type '"body"'.
当我转到函数定义时,我得到:
/**
* Constructs a `GET` request that interprets the body as a `Blob` and
* returns the full `HTTPResponse`.
*
* @param url The endpoint URL.
* @param options The HTTP options to send with the request.
*
* @return An `Observable` of the `HTTPResponse` for the request,
* with the response body as a `Blob`.
*/
get(url: string, options: {
headers?: HttpHeaders | {
[header: string]: string | string[];
};
observe: 'response';
params?: HttpParams | {
[param: string]: string | string[];
};
reportProgress?: boolean;
responseType: 'blob';
withCredentials?: boolean;
}): Observable<HttpResponse<Blob>>;
我不明白我做错了什么。你能帮我吗 ?
谢谢你的帮助 !
解决方案
您正在使用该函数的通用版本,但正在查看非通用文档。没有接受的通用重载observe: 'response', responseType: 'blob'
。
相反,使用非通用版本:
const header = {Authorization: 'Bearer ' + token};
const config = {headers: header, responseType: 'blob' as 'blob', observe: 'response' as 'response'};
return this.http.get(url, config);
所有的泛型重载只允许responseType: 'json'
.
来源:https ://github.com/angular/angular/blob/master/packages/common/http/src/client.ts
推荐阅读
- excel - 自动打开 Excel 工作簿,但前提是它尚未打开
- groovy - 有没有办法为 groovy 脚本指定构造函数?
- python - Python Pandas 将数据移动到新行
- javascript - 如何覆盖默认验证器的默认消息?
- java - GWT SimplePager如何刷新
- php - 自定义数据库和wordpress数据库之间的连接
- c# - 如何删除 StyleCop 警告“此异步方法缺少 'await' 运算符并将同步运行”而不从签名中删除异步
- emacs - 在 emacs 中跨不同修订树导航到目录的最快方法
- javascript - 我无法使用 peerjs Webrtc 在 Safari 浏览器上接收或发出视频流
- http - 在 Flutter 应用中获取 http get 请求的 URL