【问题标题】:typescript error - Types of property 'headers' are incompatible打字稿错误 - 属性“标题”的类型不兼容
【发布时间】:2018-11-18 14:15:47
【问题描述】:

我的 Angular 应用程序有问题。当我尝试拨打 http 时收到打字稿错误。

这是错误输出:

[09:36:28] 打字稿:C:/xampp/htdocs/project x/anonymous-social/src/pages/home/home.ts,行:184 '{ headers: HttpHeaders; 类型的参数}' 不可分配给“RequestOptionsArgs”类型的参数。 属性“标题”的类型不兼容。类型“HttpHeaders”不可分配给类型“Headers”。 “HttpHeaders”类型中缺少属性“forEach”。

L183: this.http.post(url, JSON.stringify(payload),

L184: {headers: new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'})})

L185: .map((res:Response) => {

我不完全确定它不喜欢什么..

这是我的实际代码:

我正在同时加载 http/common/http

import { HttpHeaders } from "@angular/common/http";
import { Http, Response } from "@angular/http";

然后在构造函数中:

private http: Http,

最后是我的 js:

getCall(url, payload, text) {
  this.http.post(url, JSON.stringify(payload),
  {headers: new HttpHeaders({'Content-Type': 'application/json; charset=utf-8'})})
  .map((res:Response) => {
    console.log(res)
})

任何想法我做错了什么?任何帮助将不胜感激!

【问题讨论】:

    标签: javascript angular typescript


    【解决方案1】:

    这是我设置标题的方式,我使用的是Headers,而不是HttpHeaders

    import { Http, Response, Headers, RequestOptions } from "@angular/http";
    
    // ...
    
    const headers = new Headers({ 'Content-Type': 'application/json; charset=utf-8' });
    const options = new RequestOptions({ headers: headers });
    
    // ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-12
      • 2021-02-19
      • 2017-03-26
      • 2020-08-13
      • 1970-01-01
      • 2018-05-16
      • 2021-10-01
      • 1970-01-01
      相关资源
      最近更新 更多