Header 中获取。 Angular 6

Header in get. Angular 6

我正在做一个我从同一个网站上获取的示例,但我不知道我做错了什么会给我一个错误,即使我看了它我也看不到什么我做错了。

在执行 "response => response.json()" 时出现以下错误:

The json property does not exist in the Object type

我的服务如下:

import { Injectable } from '@angular/core';
import { HttpHeaders,HttpClient  } from '@angular/common/http';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Injectable({
  providedIn: 'root'
})
export class CompetenceService {

  public url: string;

  constructor(private httpCliente: HttpClient) {}

  public createUser(): Observable < any > {
    const getHeaders: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.httpCliente
      .get('http://xxx/xxx/xxx', {
        headers: getHeaders
      }).pipe(
        map(
          response => response.json() < --error: The 'json'
          property does not exist in the 'Object'
          type
        )
      );
  }
}

有人看到我做错了

您不需要 map 它,因为您正在使用 HttpClient。仅当您使用 @angular/http

中的 Http 时才需要

HttpClient 为您提供实际的响应数据。因此,您不必像在使用 Http 时那样对响应调用 json 来获取实际数据。如果这样做,由于响应数据上不会有名为 json 的方法,它会抛出错误。这就是它所做的。

摆脱它,你应该没事。

import { Injectable } from '@angular/core';
import { HttpHeaders, HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';

@Injectable({
  providedIn: 'root'
})
export class CompetenceService {

  public url: string;

  constructor(private httpCliente: HttpClient) {}

  public createUser(): Observable < any > {
    const getHeaders: HttpHeaders = new HttpHeaders({
      'Content-Type': 'application/json'
    });
    return this.httpCliente
      .get('http://xxx/xxx/xxx', {
        headers: getHeaders
      });
  }
}