6

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

我的问题是,如果我想在组件中使用它的只读子集,我应该在哪里过滤状态?

我是在减速器、服务或组件中执行此操作吗?

4

1 回答 1

3

可以在ngrx 示例应用程序中找到一些指导。有一种模式,其中选择器与 reducers 一起定义:

/**
 * 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());
  }
}

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

于 2016-08-09T01:35:40.290 回答