【问题标题】:Angular argument is not assignable (Httpclient)角度参数不可分配(Httpclient)
【发布时间】: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


【解决方案1】:

文档说你可以传递 HttpRequest 类型。因此,当您构建它时,通过它。

  const req = new HttpRequest('GET', url, {
      reportProgress: true
    });

    return this.http.request(req).pipe(
     map(event => this.getEventMessage(event, file)),
      retry(3),
      catchError(this.handleError));

【讨论】:

    猜你喜欢
    • 2020-10-16
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-26
    相关资源
    最近更新 更多