0
votes

I'm using Angular 6. When i want to use route parameters, I use this code:

this.route.params.subscribe(params => {
    // use params now as a object variable
});

But I need to use parameters in service calling. Example I have a service for getting user info. And I call this service inside of resolver. If I want to use subscribe, I can not return service result:

resolve(route: ActivatedRouteSnapshot,state: RouterStateSnapshot): Observable<any> {
    this.route.params.subscribe(params => {
        return this.FixturesService.getFixtures(params);
    });    
});

It's not working now. It says:

A function whose declared type in neither 'void' nor 'any' must return a value.

How can I return service result, inside of the resolve function with passing route parameters to the service that I called and return it to component?

1

1 Answers

1
votes

You can solve this by using switchMap to change your route params observable to the observable of your service:

resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<any> {
  return route.params.switchMap(params => this.FixturesService.getFixtures(params));
}

The service call will not be made until the params are resolved, and then subscribers will receive the result of the service call.

Thanks to @JBNizet for the correction.