Angular 角度5 HTTPclient json错误
我正在尝试从Angular 5 ionic项目中的json文件导入数据,但我无法导入,因为我遇到错误: TS2339:home中的类型“Object”上不存在属性结果。共有2个属性结果 提供者:people.ts home.ts 正如我在angular/http指南中看到的,应该是这样的:Angular 角度5 HTTPclient json错误,angular,ionic-framework,angular5,Angular,Ionic Framework,Angular5,我正在尝试从Angular 5 ionic项目中的json文件导入数据,但我无法导入,因为我遇到错误: TS2339:home中的类型“Object”上不存在属性结果。共有2个属性结果 提供者:people.ts home.ts 正如我在angular/http指南中看到的,应该是这样的: showConfig() { this.configService.getConfig() .subscribe(data => this.config = { heroes
showConfig() {
this.configService.getConfig()
.subscribe(data => this.config = {
heroesUrl: data['heroesUrl'],
textfile: data['textfile']
});
}
但我想复制所有的数据,而不仅仅是少数几个部分。正确的方法是什么?在您的people.ts上为map操作符添加导入
import 'rxjs/add/operator/map';
getPeople() {
return this.http.get('http://www.json-generator.com/api/json/get/cghlMAGJvm?indent=10').map(res => res.json());
}
来自的响应已经是结果,因此无需添加data.results,该属性在响应上不存在
this.service.getPeople().subscribe( data => {
this.people = data
});
浏览器中也出现错误:错误错误:未捕获承诺:类型错误:this.service.getPeople…订阅不是函数类型错误:this.service.getPeople…订阅不是函数请给出一个答案。另外,我建议你阅读。我只复制了“结果”所在的部分。“results”这个词出现了两次,并且写了另一个我在浏览器上看到的错误。两者都在我的代码中。不多也不少。是的,我看到了angular的http指南,做得很好。你没有做得很好,你没有键入任何东西。因此编译器抱怨。你能告诉我我必须做什么吗?谢谢,它确实有效,但还有一个问题->它可以使用:这个json,但不能使用我需要的这个:,为什么?浏览器错误:错误错误:尝试区分“[object]”时出错。只允许数组和iterables修复所有问题,再次提醒您。
import 'rxjs/add/operator/map';
getPeople() {
return this.http.get('http://www.json-generator.com/api/json/get/cghlMAGJvm?indent=10').map(res => res.json());
}
this.service.getPeople().subscribe( data => {
this.people = data
});