0

I'm interested in adapting the following code for route resolvers.

    private categorySubject = new Subject<number>();
    categorySelectedAction$ = this.categorySubject.asObservable();
    
    products$ = this.categorySelectedAction$.pipe(
     switchMap(catId=>this.http.get<Product[]>(`${this.url}?cat=${catId}`))
        .pipe(
          tap(data => console.log(data)),
          catchError(this.handleError)
      ));
    
    selectedCategoryChanged(categoryId: number): void {
      this.categorySubject.next(categoryId);
    }

Should the resolver call selectedCategoryChanged and the return products$? My attempts to accomplish this do not return anything.

3
  • I mean, yes, you can just call selectedCategoryChanged method in resolver, just return any value from resolver to not block the navigation. But more important question - why do you want to do this in resolver? Resolver primarily solves the issue when you want to load data as soon as possible, so you could just call http.get directly in resolver and bind the returned value to the component via input or route.data. Why do you want to move this certain logic to resolver and not return anything from it? Commented Jul 13, 2024 at 7:38
  • I want all data calls to flow through a service, in this case, the product service. Similarly, when I want to refresh the displayed product data I want to use the product service. I may have to call the api service directly from the resolver because the approach above using a subject is not properly hydrating the product view. Thanks for the suggestion. Commented Jul 13, 2024 at 16:55
  • I've abandoned trying to use resolvers with declarative rxjs. I just cannot bind the template's data source to the results from the resolver and then to a different source as data is refreshed or as the user changes selection parameters. Commented Jul 14, 2024 at 0:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.