在哪里过滤状态?

6

新手问题:我有一个使用ngrx的angular2应用程序,我有一个服务返回状态(一组可观察对象)给组件。

我的问题是,如果我想在组件中使用只读子集进行过滤,那么我在哪里过滤状态?

我应该在reducer、service还是component中做呢?

1个回答

3

可以在ngrx示例应用中找到一些指导。有一个模式是定义选择器与reducer一起定义

/**
 * Because the data structure is defined within the reducer it is optimal to
 * locate our selector functions at this level. If store is to be thought of
 * as a database, and reducers the tables, selectors can be considered the
 * queries into said database. Remember to keep your selectors small and
 * focused so they can be combined and composed to fit each particular
 * use-case.
 */
export function getBookEntities() {
  return (state$: Observable<BooksState>) => state$
    .select(s => s.entities);
};

这些选择器被用于(智能)组件中来选择/过滤状态:

在这里查看代码示例
...
export class CollectionPage {

  books$: Observable<BooksInput>;

  constructor(store: Store<AppState>) {
    this.books$ = store.let(getBookCollection());
  }
}

这种模式/机制可以用于过滤组件或服务中的状态 - 无论哪种最适合您的架构。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接