在我的Angular 4应用程序中,我正在尝试从API获取一些数据。我正在使用this文章解释如何做到这一点,但我得到了一个例外:
TypeError:this.http.get(...)。map不是函数
这是我的代码:
import { Injectable } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
import { Person } from '../../../interfaces/Person';
import {configuration} from "../config";
@Injectable()
export class AdsService{
private baseUrl: string = configuration.serverUrl;
constructor(private http : Http){
}
getAll(): Observable<Person[]>{
let people$ = this.http
.get(`${this.baseUrl}/people`, {headers: this.getHeaders()})
.map(mapPeople);
return people$;
}
}
function mapPeople(response:Response): Person[]{
return response.json().results;
}
任何帮助都将深表感谢!
答案 0 :(得分:2)
我没有看到导入的地图方法。
将此添加到您的导入
import rxjs/add/operator/map
答案 1 :(得分:0)
您必须在此文件中导入地图,因为您只需添加以下行:
import 'rxjs/add/operator/map';
OR
import 'rxjs/add/operators/map';
此外,如果您使用的是Angular 5或更高版本,则上述导入已排序为:
import { map } from 'rxjs/operator';
OR
import { map } from 'rxjs/operators';
添加此并享受! : - )