2 回答
![?](http://img1.sycdn.imooc.com/54585094000184e602200220-100-100.jpg)
TA贡献1854条经验 获得超8个赞
为什么不使用 switchMap?
parentHttpService.saveOrUpdateStudent(studentDetail: IStudentDetail).pipe(
switchMap(res=>{
..here you has the response of saveStudent
..I supouse you want to change "parentDetail", with the response
..e.g. if return a studentId, you can write some like
parentDetail.studentId=res.studentId
return parentHttpService.saveOrUpdateParent(parentDetail)
})).subscribe(res=>{
..here the response of saveParent..
})
![?](http://img1.sycdn.imooc.com/545862e700016daa02200220-100-100.jpg)
TA贡献1712条经验 获得超3个赞
你不需要 async 或 await 在这里。你可以做这样的事情(稍微简化一下,所以它可能需要一些工作):
saveStudentAndParent(): void {
studentDetail.saveStudent().pipe(takeUntil(this._destroy$)).subscribe(
data => this.saveParent(data),
err => console.log(err)
)
}
saveParent(data: any): void {
// Do your check if the student saved successfully then save parent
}
你会注意到我使用 takeUntil 来确保取消订阅,这需要更多的管道,然后我在这里开始工作,但相当简单。您所要做的就是设置一个名为 destroy$ 的主题并在 ngOnDestroy 中调用
this._destroy$.next(true);
this._destroy$.complete();
在保存学生调用的订阅中调用 saveParent 可确保在继续之前完成学生调用。
添加回答
举报