Richard77
Richard77

Reputation: 21609

How to inject the NGRX store in a functional angular resolver

I'm trying to convert my class-based resolver to a functional one. Injecting NGRX store is not working.

export const loadCategoriesParentCategoryCodeResolver
  : ResolveFn<boolean> = (
  route, 
  state,
  store: Store<any> = Inject(Store<any>)) => {
  let loading = false;

  return store.pipe(
    tap(() => {
    if(!loading){
     loading = true;
     store.dispatch(
      loadCategoriesByParentCategoryCodeRequest({ 
        parentCategoryCode: null }))
     }
   }),
   first(),
   finalize(() => loading = false)
  );
 };

In a class-based resolver, I would just inject the store in the constructor.

constructor(private _store: Store<Any>){}

Unfortunately, I'm getting the error that pipe doesn't exist. I've tried both store: Store<any> = Inject(Store<any>, but it is not working.

Upvotes: 0

Views: 1056

Answers (1)

jmeinlschmidt
jmeinlschmidt

Reputation: 1476

See the ResolveFn documentation.

export const loadCategoriesParentCategoryCodeResolver: ResolveFn<boolean> = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
  const store = inject(MyStore); // Change MyStore according to your store DI token
  let loading = false;

  return store.pipe(
    tap(() => {
    if(!loading){
     loading = true;
     store.dispatch(
      loadCategoriesByParentCategoryCodeRequest({ 
        parentCategoryCode: null }))
     }
   }),
   first(),
   finalize(() => loading = false)
  );
 };

Upvotes: 0

Related Questions