Search code examples
angularngrx

NGRX selector in resolver


In my resolver I have:

export class ComicMainResolver implements Resolve<any> {
  store = new Observable<any>();

  resolve(): Observable<any> {
    return this.store.pipe(select(getItems(CategoryType.Comics, false, 'results')))
      .pipe(
        filter((comic: any) => {
          console.log(comic);
          return comic !== undefined;
        }), first());
  }
}

But it does not resolve, won't even log the var in the filter


Solution

  • The problem is that store is an empty Observable, instead, you need to inject your store and use it like this:

    @Injectable({
      providedIn: 'root'
    })
    export class ComicMainResolver implements Resolve<any> {
      constructor(private store: Store<State>) {}
    
      resolve(): Observable<any> {
        return this.store.pipe(select(getItems(CategoryType.Comics, false, 'results')))
          .pipe(
            filter((comic: any) => {
              console.log(comic);
              return comic !== undefined;
            }), first());
      }
    }