一些指导可以在ngrx 示例应用程序中找到。有一种模式将选择器与减速器一起定义:
/**
* 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);
};
Run Code Online (Sandbox Code Playgroud)
这些选择器在(智能)组件中使用来选择/过滤状态:
...
export class CollectionPage {
books$: Observable<BooksInput>;
constructor(store: Store<AppState>) {
this.books$ = store.let(getBookCollection());
}
}
Run Code Online (Sandbox Code Playgroud)
这种模式/机制可用于过滤组件或服务中的状态 - 无论哪种方式最适合您的架构。
| 归档时间: |
|
| 查看次数: |
1083 次 |
| 最近记录: |