DEV Community

Discussion on: Where to initialize components selectors in Angular?

Collapse
 
maxime1992 profile image
Maxime

Instead of having one line for each declaration and another for assignment, I prefer to do all that at once. Here's how your example would look like:

export class FindBookPageComponent {
  searchQuery$: Observable<string> = this.store.pipe(
    select(fromBooks.selectSearchQuery),
    take(1)
  );
  books$: Observable<Book[]> = this.store.pipe(
    select(fromBooks.selectSearchResults)
  );
  loading$: Observable<boolean> = this.store.pipe(
    select(fromBooks.selectSearchLoading)
  );
  error$: Observable<string> = this.store.pipe(
    select(fromBooks.selectSearchError)
  );

  constructor(private store: Store<fromBooks.State>) {}

  search(query: string) {
    this.store.dispatch(FindBookPageActions.searchBooks({ query }));
  }
}