24

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 !

KCarnaille
  • 1,027
  • 9
  • 18
  • 1
    Since your data changes according to your queryParams, the subscription to the queryParams must be in the ngOnInit. Resolver are good for Data that does not change. – Radouane ROUFID Jul 21 '17 at 10:07
  • Yes, that's what is implemented at the moment. My problem is that i've got ~ 8 api calls that are supposed to build many things. I don't think there is a solution to my " issue ", but I prefer asking.. – KCarnaille Jul 21 '17 at 10:09
  • Did you tried to navigateByUrl ? I think this forces the component the re-resolve the data. Note : this does not solve the problem of your 8 api calls – Radouane ROUFID Jul 21 '17 at 10:10
  • I'm not very familiar with navigateByUrl. I'm reading some doc, thanks :) – KCarnaille Jul 21 '17 at 10:12

1 Answers1

48

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.

Max Koretskyi
  • 101,079
  • 60
  • 333
  • 488
  • Waw, never heard of this. I read the official doc, I think it's exactly what I need. I'm gonna test it and edit my answer if I can reach what I want, thank you !! – KCarnaille Jul 21 '17 at 12:56
  • I tried, is works PERFECTLY. Exactly what I needed ! I edit my question with your answser. Many thanks – KCarnaille Jul 21 '17 at 13:08