在我放入(编辑)一些记录之前,我会从数据库中获得新的数据。
服务:
getVotes(): Observable<Vote> {
return this._http.get<Vote>(url);
}
putVote(choose: string){
this.getVotes().subscribe(
result => {
...
return this._http.put<Vote>(url + object).subscribe();
}
)
}现在它可以工作了,但我认为putVote()应该返回Observable<Vote>,那么如何从内部getVotes()返回
发布于 2018-08-17 18:53:13
您应该使用switchMap映射来自getVote()的响应,并从http.put()中删除subscribe以从putVote()返回Observable
putVote(choose: string){
return this.getVotes().pipe(switchMap(
result => {
...
return this._http.put<Vote>(url + object);
}
))
}发布于 2018-08-17 18:56:11
你能试试下面的代码吗?
getVotes(): Observable<Vote> {
return this._http.get<Vote>(url);
}
putVote(choose: string): Observable<Vote> {
this.getVotes().subscribe(
result => {
...
return this._http.put<Vote>(url + object).subscribe();
}
)
}https://stackoverflow.com/questions/51893747
复制相似问题