【发布时间】:2023-04-02 12:25:02
【问题描述】:
我正在使用“@angular/common”:“^6.0.7”,文档说 httclient.request 可以接受字符串或 HttpRequest。然而,当我传入 HttpRequest 时,我得到错误 TS2345: Argument of type 'HttpRequest' 不能分配给“字符串”类型的参数。
下面是代码。
const req = new HttpRequest('GET', url, {
reportProgress: true,
observe: 'response'
});
return this.http.request(
req, url, {
observe: 'response'
}).pipe(
retry(3),
catchError(this.handleError));
谢谢。
解决方案
查看使用的功能。就我而言
return this.http.request(
req.pipe(
retry(3),
catchError(this.handleError));
【问题讨论】:
-
如果从 return 语句中删除
url会怎样。return this.http.request(req, {observe: 'response'})... -
现在它给出了“HttpRequest”类型的参数不可分配给“字符串”类型的参数。
-
请求方法有17个重载。其中一个(文档中列出的第一个)将 HttpRequest 作为参数,它是该方法的唯一参数。但是你传递了 3 个参数。 url 和选项在 HttpRequest 中已经指定。调用方法时为什么要第二次传递它们?
-
是的,你是对的。现在可以了。没看到。
标签: angular typescript