Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rerun resolvers when queryParams change

Tags:

I'm working on it for days, But doesn't find any solution so far.

I've got a resolver service, which's supposed to return an Observable :

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) : Observable<any>|Promise<any>{     return Observable.forkJoin(          this.http.get(first call with queryParam),          this.http.get(second call with queryParam),                  ); } 

I subscribe to my ActivatedRoute data in the component, and it's working pretty well.

BUT, as you can see in the code above, my component has a queryParam, and my API calls depend on this queryParam. When I manually change it, it doesn't "resolve" the route again.

I completely understand why, I read the documentation.

Does anyone has a workaround ? Is this even possible to subscribe to params and return an Observable in this situation ?

I tried this so far, but it doesn't return an Observable, so it does nothing :

this.router.events.subscribe(event => {      if(event instanceof ResolveEnd){       // My previous Observable.forkJoin     } }); 

Thank you :) !

----------- SOLUTION (thanks Maximus) : -----------

Add in the routing :

{    path: 'some path',    runGuardsAndResolvers: 'paramsOrQueryParamsChange'    ... } 

Then, no need to subscribe to route event or anything ! Magic !

like image 220
KCarnaille Avatar asked Jul 21 '17 10:07

KCarnaille


1 Answers

You can use runGuardsAndResolvers for the route. Here is what the official docs say:

... defines when guards and resolvers will be run. By default they run only when the matrix parameters of the route change. When set to paramsOrQueryParamsChange they will also run when query params change. And when set to always, they will run every time.

So when defining a route you can use it like this:

{    path: 'some path',    runGuardsAndResolvers: 'paramsOrQueryParamsChange'    ... } 

This should re-run guards and resolvers.

like image 120
Max Koretskyi Avatar answered Sep 22 '22 03:09

Max Koretskyi