Angular 4/5 HttpClient:字符串类型的参数不可分配给“body" [英] Angular 4/5 HttpClient: Argument of type string is not assignable to 'body'

查看:28
本文介绍了Angular 4/5 HttpClient:字符串类型的参数不可分配给“body"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Angular 文档说:

The Angular docs say:

响应正文不会返回您可能需要的所有数据.有时服务器返回特殊标头或状态代码以指示某些条件,并检查这些可能是必要的.为此,您可以告诉 HttpClient 你想要完整的响应,而不仅仅是正文使用观察选项:

The response body doesn't return all the data you may need. Sometimes servers return special headers or status codes to indicate certain conditions, and inspecting those can be necessary. To do this, you can tell HttpClient you want the full response instead of just the body with the observe option:

http
  .get<MyJsonData>('/data.json', {observe: 'response'})
  .subscribe(resp => {
    // Here, resp is of type HttpResponse<MyJsonData>.
    // You can inspect its headers:
    console.log(resp.headers.get('X-Custom-Header'));
    // And access the body directly, which is typed as MyJsonData as requested.
    console.log(resp.body.someField);
  });

但是当我尝试这样做时,我收到一个编译时间错误(虽然没有运行时错误,但按预期工作):

But when I try that, I get a compilation time error (no runtime errors though, works as expected):

错误 TS2345:类型为 '{ headers: HttpHeaders; 的参数;观察:字符串;}' 不能分配给类型为 '{ headers?: HttpHeaders | 的参数{ [标题:字符串]:字符串 |细绳[];};观察?:身体";参数?:Ht...'.属性观察"的类型是不兼容的.类型 'string' 不能分配给类型 '"body"'.

error TS2345: Argument of type '{ headers: HttpHeaders; observe: string; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: Ht...'. Types of property 'observe' are incompatible. Type 'string' is not assignable to type '"body"'.

为什么?我使用 "@angular/http": "^5.1.0"

这是我的代码版本:

  login(credentials: Credentials): Observable<any> {
    const options = {
      headers: new HttpHeaders({'Content-Type': 'application/json'}),
      observe: 'response'
    };
    return this.httpClient.post<any>(`${environment.USER_SERVICE_BASE_URL}`,
      {'username': credentials.username, 'password': credentials.password}, options)
      .map((res) => ...

推荐答案

您必须内联选项.见 github 票 #18586,8 月 9 日由 alxhub 输入2017.

You have to inline the options. See github ticket #18586, entry by alxhub on August 9 2017.

Typescript 需要能够静态推断observe 和responseType 值,以便为get() 选择正确的返回类型.如果您传入一个类型不正确的选项对象,它就无法推断出正确的返回类型.

Typescript needs to be able to infer the observe and responseType values statically, in order to choose the correct return type for get(). If you pass in an improperly typed options object, it can't infer the right return type.

login(credentials: Credentials): Observable<any> {
    return this.httpClient.post<any>(`${environment.USER_SERVICE_BASE_URL}`,
      {'username': credentials.username, 'password': credentials.password}, {
      headers: new HttpHeaders({'Content-Type': 'application/json'}),
      observe: 'response'
    })
      .map((res) => ...

这篇关于Angular 4/5 HttpClient:字符串类型的参数不可分配给“body"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆