【发布时间】:2021-02-23 09:45:49
【问题描述】:
我有以下代码
@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 返回错误,则执行错误部分,之后当行为主体(选择用户)发生变化时,它不会调用用户/获取。是否有其他方法来处理 behaviorsubject 的错误或任何其他方法来处理这个想法。在这种情况下应该如何使用行为主体。
【问题讨论】:
标签: angular7 rxjs6 behaviorsubject