Angular5到Angular6:Property 'map' does not exist on type 'Observable'.

错误原因:angular6不再支持原来的Http,用HttpClient代替了原来的Http

解决方案:

1. import

import {Http, Headers, Response} from '@angular/http';  

改为=>

import {HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';

2.constructor

constructor(
  private httpService: Http
) {}

改为=>

constructor(
  private httpClient: HttpClient
) {}

3.具体用法

const headers = new Headers({
  'Content-Type': 'application/json',
  'x-access-token': localStorage.getItem('token') ? localStorage.getItem('token') : ''
});
return this.httpService.get(environment.baseUrl + 'users/myheadshot',
  {headers: headers})
  .map((response: Response) => {
    return response.json();
  });

改为=>

const headers = new HttpHeaders({
  'Content-Type': 'application/json',
  'x-access-token': localStorage.getItem('token') ? localStorage.getItem('token') : ''
});
return this.httpClient.get(environment.baseUrl + 'users/myheadshot', {headers: headers});

注意:在换成httpClient后,必须指定返回类型,不然会报错,返回类型紧接get/post...之后,用尖括号括起来,比如httpClient.get,当不知道返回类型是什么时,用any。

你可能感兴趣的:(Angular)