gpt4 book ai didi

angular - 在 Angular 中使用 httpclient.post 发送和接收不同的类型

转载 作者:搜寻专家 更新时间:2023-10-30 21:22:24 24 4
gpt4 key购买 nike

我需要调用一个 Web 服务,在该服务中我发送一个带有 User 对象的 POST 请求,它返回一个 LogToken 对象。

我找不到如何使用 Angular httpClient 执行此操作,因为它似乎期望发送和接收的类型相同。如果我执行以下操作,则会出现错误:

  public login(user: User): Observable<LoginToken> {
return this._httpClient.post<User>(this._authenticationServiceUrl, user);
}

Error TS2322 Type 'Observable{User}' is not assignable to type 'Observable{LoginToken}'.

由于我对这项技术还很陌生,所以我想我做错了什么,但我不确定是什么。从 POST 请求中接收不同的类型是错误的吗?

最佳答案

传递给 post 的类型参数是您希望返回的参数。也一样

public login(user: User): Observable<LoginToken> {
return this._httpClient.post<LoginToken>(this._authenticationServiceUrl, user);
}

关于angular - 在 Angular 中使用 httpclient.post 发送和接收不同的类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49129846/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com