Moh*_*umb 5 javascript php angular
我的服务:
fetchData(url,request)
{
return this.http.post(this.apiUrl+url,request).subscribe(
(response) => {return response.json()}
);
}
Run Code Online (Sandbox Code Playgroud)
我的组件:
ngOnInit()
{
this.response = this.dataService.fetchData('friends/grow_network',{});
console.log('s ',this.response);
}
Run Code Online (Sandbox Code Playgroud)
如果我在服务中控制台response.json(),它将显示来自api的正确数据,但是如果我在组件中进行控制台,则显示如下:
Subscriber {closed: false, _parent: null, _parents: null, _subscriptions: Array(1), syncErrorValue: null…}
Run Code Online (Sandbox Code Playgroud)
如何获取来自api而不是订户数据的组件中的数据?
当按照您的方式进行操作时,您在变量“响应”中写入可观察对象而不是结果。调用该方法时,结果值尚未出现,因为它将是异步的。
要获得您想要的,您必须执行以下操作:
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);
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4860 次 |
| 最近记录: |