angular - 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()
?
解决方案
您应该映射来自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);
}
))
}
推荐阅读
- python - 在 QTableWidget 单元格 PyQt5 的标签中复制粘贴文本
- python - 为什么在执行列表理解时会在列表中看到布尔值?
- javascript - 事件侦听器中的 setTimeout
- arrays - 无法将输入数组从形状 (150,150,3) 广播到形状 (150,150,3,3)
- html - 如何使表格内的div填满剩余空间
- xamarin - 从 Rest API 到 Picker Xamarin.Forms 的绑定结果
- bash - 在linux中导出环境变量不适用于变量
- selenium - 从不同的 Step 定义类访问 webdriver 时的空指针
- python - 给定数据 URI 的 Flask 返回文件
- php - php 错误请求保护