【发布时间】:2018-01-16 17:26:46
【问题描述】:
我创建了一个从中获取数据的服务,但遇到以下错误:
ERROR TypeError: this.http.get(...).map 不是函数
我正在使用HttpClient 模块。这是我的脚本:
import {Injectable} from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class Api{
public data: any = [];
constructor(private http: HttpClient){ }
getPartnersName(){
return this.http.get('http://aff.local/getPartners.php', {
observe: 'response',
responseType: 'json'
}).map(
(data)=>{
console.log(data)
},
error=>{
console.log(error);
}
);
}
}
编辑
我仍然看不到组件中的数据:
ngOnInit(){
this.api.getPartnersName().subscribe(
(data)=>{
this.data = data;
}
);
}
【问题讨论】:
-
HttpClient 中的默认响应类型是
json如果要控制台,则不需要使用map。使用do -
HttpClient实现已经包括map的内部使用......它会自动为您工作。参考这个链接github.com/angular/angular/issues/20460
标签: angular httpclient angular5