如何解决与合并映射一起使用时的 Rxjs BehaviorSubject 错误处理
我有以下代码
@Injectable()
export class ReceptionService {
private generalInfoDataSrc$ = new BehaviorSubject<any>(null);
public generalInfoData = this.generalInfoDataSrc$.asObservable();
setGeneralInfo(dataSrc: GeneralInfoMModal) {
this.generalInfoDataSrc$.next(dataSrc);
}
}
从我的 component1 中,我将上述设置为
OnSelect(patient: any) {
let generalInfo = new GeneralInfoMModal();
generalInfo.id = patient.id;
// some other code here
// this.recepService.setGeneralInfo(generalInfo);
}
// from component2
//
ngOnInit() { getPatientDetails() }
getPatientDetails() {
this.receptionService.generalInfoData.pipe(mergeMap(response => {
if (response && response.id) {
this.loading = true;
return this.receptionService.get('User/Get' + response.id,this.sourceobj);
} else {
return of(null);
}
}),takeUntil(this.unsubscribe$)).subscribe(response => {
this.patient = response;
this.loading = false;
},error => {
this.loading = false;
// this.utility.showMsg('An error occurred while getting user.')
},() => {
})
}
一切正常。我继续选择用户,从而调用 User/Get api。但是,如果 api 返回错误,则执行错误部分,然后当行为主体(用户被选择)发生变化时,它不会调用 User/Get。是否有其他方法来处理带有行为主题的错误或任何其他方法来处理这个想法。在这种情况下应该如何使用行为主体。
解决方法
如果反复使用同一个行为主体,并且出现错误,则需要将行为主体重新设置为null,这样下一个用户设置时,会得到最新的值。
尝试这样的事情:
getPatientDetails() {
this.receptionService.generalInfoData.pipe(mergeMap(response => {
if (response && response.id) {
this.loading = true;
return this.receptionService.get('User/Get' + response.id,this.sourceobj);
} else {
return of(null);
}
}),takeUntil(this.unsubscribe$)).subscribe(response => {
this.patient = response;
this.loading = false;
},error => {
this.loading = false;
///////////////////////////////// ADD THIS LINE ///////////////////////
this.recepService.setGeneralInfo(null);
// this.utility.showMsg('An error occurred while getting user.')
},() => {
})
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。