[英]Angular2/Rxjs nested maps with conditions in guard
I'd stuck with Rxjs operators. 我坚持使用Rxjs运算符。
This is a part of Angular's guard canActivate
这是Angular的后卫canActivate
的一部分
const ifNoPatientCondition$ = this.dataService.getList().map(pl => {
console.log('im here'); // <<< this message not showing
const found = findOnlyAvailablePatients(pl);
if (found[0] === 1) {
this.stateService.patient.setCurrent(found[1]);
this.dataService.getPatientData(pid);
// return Observable.of(true);
return true;
} else {
if (found[0] === 0) {
this.stateService.app.message.send('Wrong patient status');
} else if (found[0] === -1) {
this.stateService.app.message.send('Wrong patient ID');
}
this.subscribes.forEach(subscribe => subscribe.unsubscribe());
this.stateService.navigate('/patients');
// return Observable.of(false);
// return false;
}
});
const warnOkCondition$ = this.stateService.patient.getCurrent().pipe(mergeMap(pat => {
if (!pat || pat.patient_id !== pid) { // <<< i'm interested with this condition
console.log('there is no patient!', pat); // <<< i see this message
return ifNoPatientCondition$; // <<< but cannot reach this line
} else {
if (pat.status === 'TREATMENT_COMPLETE') {
return Observable.of(false);
}
return Observable.of(true);
}
}));
return warningDialog().pipe(concatMap(warningResult => {
if (!warningResult) { // <<< if clicked No
this.stateService.navigate('/patients');
return Observable.of(false);
} else { // <<< 'Yes'
console.log('you are the best');
return warnOkCondition$;
}
}));
warningDialog()
shows a dialog and returns observable of result. warningDialog()
显示一个对话框并返回可观察的结果。 If i clicked No , code works right: guard returns false and router navigate to /patients
. 如果我单击否 ,代码工作正常:guard返回false并且路由器导航到/patients
。
else if i clicked Yes , warnOkCondition$
works partially right (i'm interesting with first condition (with console.log
)): i see message in console, but cannot reach next line - ifNoPatientCondition$
code. 否则,如果我单击是 , warnOkCondition$
部分正常工作(我对第一个条件感兴趣(使用console.log
)):我在控制台中看到消息,但无法到达下一行 - ifNoPatientCondition$
code。
Thanks! 谢谢!
Please use Types if you are working with Typescript. 如果您正在使用Typescript,请使用类型。 It is not clear what is an array and what is an Observable. 目前尚不清楚什么是数组,什么是Observable。 Since warnOkCondition$
returns Observable.of(true/false)
on some conditions, I assume this.dataService.getList()
returns an Observable as well, and not a list, even though pl
has no $
-suffix at the end. 由于warnOkCondition$
在某些条件下返回Observable.of(true/false)
,我假设this.dataService.getList()
返回一个Observable,而不是列表,即使pl
没有$
-suffix。 In this case you need to subscribe to ifNoPatientCondition$
if you want it to be executed. 在这种情况下,如果您希望执行ifNoPatientCondition$
则需要订阅它。
You might want to use switchMap
or mergeMap
here. 您可能希望在此处使用switchMap
或mergeMap
。 https://netbasal.com/understanding-mergemap-and-switchmap-in-rxjs-13cf9c57c885 https://netbasal.com/understanding-mergemap-and-switchmap-in-rxjs-13cf9c57c885
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.