angular - NGXS - 从父状态中选择子状态
问题描述
我正在学习 NGXS 并且有一个具有嵌套父/子关系的状态模型,其中有一些父母,每个父母都有自己的一组孩子(和孙子),如下所示:
state: {
parents: ParentModel[]
}
ParentModel {
children: ChildModel[]
}
ChildModel {
grandchildren: GrandChildren[]
}
etc.
但是,当在 redux/flux 实现中对状态进行建模时,上面的示例(据我所知)通常会被规范化。
我希望能够以与状态树的心理模型相匹配的方式访问子孙。我想出的最好方法是按照文档(https://ngxs.gitbook.io/ngxs/concepts/select#lazy-selectors)中的描述创建一个“惰性选择器”
像这样:
export class ChildRecordState {
@Selector()
static childRecords(state: ChildRecordModel) {
return (id: number) => state.Records.filter(r => r.parentId === id);
}
// ...
}
(这里没有孙子的完整示例:https ://stackblitz.com/edit/angular-ngxs-parent-child )。
我确实看到有可以访问的子状态,我可以在父级中使用它创建一个选择器,但这似乎并没有真正改善结果。问题是 - 访问父母的孩子是否有比示例/stackblitz 中显示的更好的模式?
解决方案
推荐阅读
- json - 在 ADF 中使用 FOR JSON 子句结果从查询创建 JSON 文件
- sqlite - Sqlite INSERT OR REPLACE 正在递增 rowid
- javascript - 使用后退按钮时退出/关闭 PWA
- image - 删除 RMD 文件中图像下方的空白以进行 PDF 输出
- flutter - Flutter:如何让我定义的无状态小部件随机显示一个按钮?
- javascript - 按属性值过滤对象属性
- python-3.x - 使用类在国际象棋中制作棋子。设置棋子时遇到问题
- sql - 我可以使用 django 中的 sed 之类的 sql 数据库创建不受限制的模式吗?
- java - 使用 GeoSpark 断言失败
- r - 带有插入符号的多元线性回归中的警告