I have a problem, how to combine 2 observable in a complicated case. I show you the code as follow:
so I have 2 Observables: This 2 Observable will be used in canActivate(): Observable<boolean| UrlTree>
1: this.loadingState : Observable<LoadingState>
2: currentData : Observable<currentData>
so loadingState will return the state of loaded data, either everything ok, or error. in my case I will check firstly the loadingState, if everything is ok, if not, will return to error page. If everything is ok, then I will pipe to currentData and do some check in currentData.
my first code as follow, but it is not working
return this.loadingState.pipe(
map((loadingState) => { // operator function 1
if(loadingState.error){
// then return something.
}
}),
() => { // operator function 2
this.currentData.pipe(
map((currentData) => {
// here will check current, also return something.
})
);
}
);
this first problem is: the first map loadingState is not all code path return a value. my idea is, if loadingState.error is true, the oerator function 2 will not carried out.
I have googled also some solution such as with combineLatest or forkjson, It could also not work.
maybe you have some solution
best thx
We could use
switchMapto switch to the other observable when error isfalse.Example: https://stackblitz.com/edit/angular-ivy-gjkulg?file=src%2Fapp%2Fapp.component.ts