【问题标题】:Type Observable<boolean> is not assignable to type Observable<HttpResponse<boolean>>类型 Observable<boolean> 不可分配给类型 Observable<HttpResponse<boolean>>
【发布时间】:2018-08-22 09:04:21
【问题描述】:

我正在开发一个 Angular 6 项目,在将代码从使用 Http 更改为 HttpClient 时,我收到以下错误:

当我尝试这样做时:

 verifyEmail(email: string, verificationCode: string): Observable<HttpResponse<boolean>> {
    return this._http.get<boolean>(`${this.serviceUrl}emailVerification?email=${email}&verification_code=${verificationCode}`, { observe: 'response' }).pipe(
      map((response: HttpResponse<boolean>) => {
        return resp.status === 204;
      }),
      catchError(this.handleError));
  }

我得到了错误

Type Observable<boolean> is not assignable to type Observable<HttpResponse<boolean>>

反过来说:

deleteCollection(collection: Collection): Observable<HttpResponse<boolean>> {
    return this._http.delete<boolean>(this.serviceUrl + collection.id, this.requestOptionsResponse).pipe(
      map((response: HttpResponse<boolean>) => {
        return response.status === 204;
      }),
      catchError(this.handleError),);
  }

报告没有错误,this.requestOptionsResponse

private setRequestOptionsResponse() {
    let headers = new HttpHeaders({
      'Accept'      : 'application/json',
      'Content-Type': 'application/json'
    });
    this.requestOptionsResponse = { headers: headers, observe: 'response', withCredentials: true}
}

【问题讨论】:

  • 删除后,您的地图将被输入:(response: HttpResponse&lt;boolean&gt;)

标签: angular rxjs angular6


【解决方案1】:

无需在类型声明中包含HttpResponse

verifyEmail(email: string, verificationCode: string): Observable<boolean> {
   ...
}

【讨论】:

  • 谢谢。奇怪,考虑到官方 Angular 文档说getConfigResponse(): Observable&lt;HttpResponse&lt;Config&gt;&gt; { return this.http.get&lt;Config&gt;( this.configUrl, { observe: 'response' }); }
  • @Aydex 在observe 选项给出时引用reading the full response
  • 是的,我在请求中提供了该选项,但我想我缺少一些东西。
猜你喜欢
  • 2017-06-29
  • 2019-05-21
  • 1970-01-01
  • 1970-01-01
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
相关资源
最近更新 更多