How to restart receivers when changing request parameters

I've been working on it for a few days now but haven't found any solution yet.

I have a resolver service that should 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 a component and it works really well.

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

I totally understand why, I read the documentation.

Anyone have a workaround? Is it even possible to subscribe to parameters and return an observable in this situation?

I've tried this so far, but it doesn't return Observable, so it doesn't do anything:

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

      

Thank:)!

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

Add to routing:

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

      

Then there is no need to subscribe to a route event or anything else! Magic!

+3


source to share


1 answer


You can use runGuardsAndResolvers

for route. Here's what the official docs say:

... determines when protectors and resolvers are triggered. By default, they run only when the parameters of the route matrix are changed. When set to paramsOrQueryParamsChange, they will also be executed when query parameters change. And when they are always set, they will run every time.

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



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

      

This should restart the protective devices and converters.

+7


source







All Articles