26

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 !

4
  • 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. Commented Jul 21, 2017 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.. Commented Jul 21, 2017 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 Commented Jul 21, 2017 at 10:10
  • I'm not very familiar with navigateByUrl. I'm reading some doc, thanks :) Commented Jul 21, 2017 at 10:12

1 Answer 1

50

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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 !!
I tried, is works PERFECTLY. Exactly what I needed ! I edit my question with your answser. Many thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.