首页 > 解决方案 > Angular/rxjs6 - 在 PUT 之前获取

问题描述

在我 PUT(编辑)一些记录之前,我会从数据库中获取新数据。

服务:

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>,那么如何从 internal 返回getVotes()

标签: angularrxjs

解决方案


您应该映射来自getVote()usingswitchMap和 Remove subscribe from的响应http.put()以返回 Observable fromputVote()

putVote(choose: string){
 return this.getVotes().pipe(switchMap(
  result => {
    ...
    return this._http.put<Vote>(url + object);
  }
 ))
}

推荐阅读