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 !
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With