Angular 如何在将承诺转换为可观察时展平嵌套的可观察对象
我对可观察的事物还比较陌生,但仍在研究它们。Angular 如何在将承诺转换为可观察时展平嵌套的可观察对象,angular,typescript,ionic-framework,Angular,Typescript,Ionic Framework,我对可观察的事物还比较陌生,但仍在研究它们。 我正在为我拥有的这个函数而挣扎。它将promise转换为observable ok,但结果返回一个嵌套的observable,因此我必须调用subscribe两次 我如何将其展平,只返回一个可观察到的值? 注意:我花了相当多的时间查看其他stackoverflow问题,但还没有完全得到答案,或者我只是没有得到答案。 我还尝试用合并映射替换开关映射 removeItem(id: string) { let updatedDefectReports
我正在为我拥有的这个函数而挣扎。它将promise转换为observable ok,但结果返回一个嵌套的observable,因此我必须调用subscribe两次 我如何将其展平,只返回一个可观察到的值? 注意:我花了相当多的时间查看其他stackoverflow问题,但还没有完全得到答案,或者我只是没有得到答案。 我还尝试用合并映射替换开关映射
removeItem(id: string) {
let updatedDefectReports: DefectReport[];
const userId = this.authService.user.uid;
return from(firebase.auth().currentUser.getIdToken().then(tokenData => {
const token = tokenData;
return this.defectReports.pipe(
take(1),
switchMap(defectReports => {
updatedDefectReports = defectReports;
const updatedDefectReportIndex = defectReports.findIndex(x => x.id === id);
updatedDefectReports.splice(updatedDefectReportIndex, 1);
return this.http.delete<any>
(`https://defect-report-1cbe3.firebaseio.com/defect-report/${userId}/${id}.json?auth=${token}`);
}),
tap(() => {
this._defectReports.next(updatedDefectReports);
})
);
}));
}
onRemoveItem(defectReport: DefectReport, slidingItem: IonItemSliding) {
this.defectReportService.removeItem(defectReport.id).subscribe(res => {
res.subscribe(() => {
}, error => {
this.notificationService.presentAlert('Problem Removing', 'Unable to remove item');
});
});
}
removietem(id:string){
让更新的缺陷报告:缺陷报告[];
const userId=this.authService.user.uid;
从(firebase.auth().currentUser.getIdToken().then)返回(tokenData=>{
const token=tokenData;
返回此.defectReports.pipe(
以(1)为例,
开关映射(缺陷报告=>{
updatedDefectReports=缺陷报告;
const updatedDefectReportIndex=defectReports.findIndex(x=>x.id==id);
拼接(updatedDefectReportIndex,1);
返回this.http.delete
(`https://defect-report-1cbe3.firebaseio.com/defect-report/${userId}/${id}.json?auth=${token}`);
}),
点击(()=>{
此._defectReports.next(更新的缺陷报告);
})
);
}));
}
onRemoveItem(缺陷报告:缺陷报告,滑动项:IonItemSlideng){
这个.defectReportService.removeItem(defectReport.id).subscribe(res=>{
res.subscribe(()=>{
},错误=>{
this.notificationService.presentAlert('删除问题','无法删除项');
});
});
}