类型'{标题的参数:标题; }'不可分配给参数:离子

时间:2018-08-30 17:09:16

标签: angular http-headers ionic3 mean-stack

这是我的代码

gettheLinkedinUserDetails(token: string) {
    let headers = new Headers();
    headers.append('Content-Type', 'application/x-www-form-urlencoded');
    headers.append('Authorization','Bearer '+token);
    this.http.get('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url)?format=json',{headers: headers})
    .subscribe(profileData => {
      alert(profileData + 'hii');
    })
  }

我正在使用HttpClientModule并且出现错误消息

Argument of type '{ headers: Headers; }' is not assignable to parameter of type '{ headers?: HttpHeaders | { [header: string]: string | string[]; }; observe?: "body"; params?: HttpParams | { [param: string]: string | string[]; }; reportProgress?: boolean; responseType?: "json"; withCredentials?: boolean; }'. Types of property 'headers' are incompatible. Type 'Headers' is not assignable to type 'HttpHeaders | { [header: string]: string | string[]; }'. Type 'Headers' is not assignable to type '{ [header: string]: string | string[]; }'. Index signature is missing in type 'Headers'. 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

使用HttpHeaders代替Headers中的@angular/common/http

gettheLinkedinUserDetails(token: string) {

  let headers: new HttpHeaders(
        { 'Content-Type': 'application/x-www-form-urlencoded' },
        { 'Authorization','Bearer ' + token });
          this.http.get('https://api.linkedin.com/v1/people/~:(id,first-name,last-name,email-address,picture-url)?format=json',
          {headers: headers})
            .subscribe(profileData => {
                alert(profileData + 'hii');
          });
      }