Angular 9 - Guard: canActivate wait until specific condition is met

user1088417

I've the following code

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

 this.userStore$ = this.store.pipe(select('userStore'));
 return this.userStore$.pipe(
   map( res => { 
     if(res) return true 
   }))
}
      

At first time res is undefined, so the canActivate resolves itself returning false. What i need is to mantain it listening until res is valorized, and then return true to proceed to the route. How i can do it? Thanks.

Sándor Jankovics

I can see you are pretty familiar with RXJS. RXJS provides filter function which can allow or deny the flow of stream based on certain condition.

If you add the filter pipe to the stream it will deny all flow based on the condition. You always have to specify the true condition which will let the flow go.

Docs https://www.learnrxjs.io/learn-rxjs/operators/filtering/filter

// you need to import the filter from rxjs operators
import {filter, map } 'rxjs/operators';

canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean> {

 this.userStore$ = this.store.pipe(select('userStore'));
 return this.userStore$.pipe(
   filter(res => res != undefined),
   map( res => { 
     if(res) return true 
   }))
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python - selenium webdriver wait until condition met

Rxjs observable wait until some condition is met

Return condition always is false into subscribe Angular CanActivate Guard

Angular canActivate Guard with API

Angular CanActivate Guard

Async HTTP call until condition is met in Angular

rxjs angular call webservice until condition met

use RXJS to keep triggering http calls in angular until a condition is met

Angular5 canactivate guard :Types of property 'canActivate' are incompatible

Angular CanActivate guard - createUrlTree relative navigation

Angular Route Guard CanActivate stops routing

Read cookie in CanActivate route guard in Angular

Angular2 CanActivate guard not working

Angular(5) - lazy loading and canActivate guard - StaticInjectorError

Using defaultIfEmpty() still not emitting in Angular canActivate guard

Angular 2 router canActivate Auth Guard

Authorization Issue with Angular Auth Guard canActivate method

angular2 wait until observable finishes in if condition

how to make spring web flux wait until specified condition met in server and then return the response

ionic Angular 9 wait until file download url is available

Awaitatility, polling until condition is met

Continuos loop until condition is not met

Do action until condition is met

Loop in JavaScript until a condition is met

Collapse Cells until a condition is met

R sample until a condition is met

Javascript looping until condition met

Repeat prompt until condition is met

Execute the loop until the condition met