gpt4 book ai didi

filter - 在哪里过滤状态?

转载 作者:太空狗 更新时间:2023-10-29 17:27:51 24 4
gpt4 key购买 nike

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

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

我是在 reducer、service 还是 component 中做的?

最佳答案

一些指南可以在 ngrx example application 中找到.有一种模式,其中定义了选择器 alongside 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);
};

那些选择器是 used in (smart) components选择/过滤状态:

...
export class CollectionPage {

books$: Observable<BooksInput>;

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

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

关于filter - 在哪里过滤状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36505711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com