我的服务:
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).subscribe(
(response) => {return response.json()}
);
}
我的组件:
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{});
console.log('s ',this.response);
}
如果我在服务中控制response.json()它显示来自api的正确数据,但如果我在组件中控制它,它显示如下:
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null…}
解决方法:
按照自己的方式进行操作时,可以在变量“reponse”中写入可观察对象,而不是结果.在调用方法时,结果的值还没有,因为它将是异步的.
要获得您想要的东西,您必须执行以下操作:
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).map(
(response) => {return response.json()}
);
}
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{})
.subscribe(result => {
this.response = result;
console.log('s ',this.response);
});
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。