Angular 捕获从服务到组件的响应?
我是角度/离子方面的新手。我正在尝试从API调用获取响应 在我的服务中,我有以下功能:Angular 捕获从服务到组件的响应?,angular,ionic-framework,Angular,Ionic Framework,我是角度/离子方面的新手。我正在尝试从API调用获取响应 在我的服务中,我有以下功能: setClockIn() { const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json', 'Authorization': 'bearer' }) }; this.http.get('http://127.0.0.1:8
setClockIn() {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'bearer'
})
};
this.http.get('http://127.0.0.1:8000/api/v1/clock/in', httpOptions)
.subscribe(data => {
console.log(data['data']);
});
}
在我的组件中,我有以下功能
ClockIn() {
this.clockService.setClockIn();
this.presentAlert(data);
}
来自console.log(data['data'])的响应代码>是来自API的消息:
“时钟已更新”
尽管我如何在同一个时钟函数中获取此数据,以便将其作为参数发送到this.presentAlert(数据)代码>
我必须使用.subscribe()
或.pipe().map()
angular的HttpClient的HTTP GET请求返回一个,因此基本上您应该从setClockIn()
方法返回可观测值,并在组件的ClockIn()
方法中订阅它
setClockIn() {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
'Authorization': 'bearer'
})
};
return this.http.get('http://127.0.0.1:8000/api/v1/clock/in', httpOptions);
}
ClockIn() {
this.clockService.setClockIn().subscribe(data => {
console.log(data['data']);
this.presentAlert(data);
});
}
你的可能复制品完全正确,那正是我要找的,谢谢: