Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/facebook/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rxjs 可观测流-一对多vs多对一_Rxjs_Observable_Ngxs - Fatal编程技术网

Rxjs 可观测流-一对多vs多对一

Rxjs 可观测流-一对多vs多对一,rxjs,observable,ngxs,Rxjs,Observable,Ngxs,当项目可以存在于不同的模块中时,如何存储项目列表 一个状态到项目还是多个状态到项目? 我假设会有很多不同的模块来存储项目,这些模块越多,我就越想离开一个状态 一个国家 类项目{ id:编号; 名称:字符串; 位置:“设备”|“杂志”|“商店”//|等未来。。。 } 类AppState{ 项目:项目[]; } 常量getItems=(存储:存储):可观察=>{ 返回存储。选择((appStore:appStore)=>appStore.items); } const getItemsInEquip

当项目可以存在于不同的模块中时,如何存储项目列表

一个状态到项目还是多个状态到项目? 我假设会有很多不同的模块来存储项目,这些模块越多,我就越想离开一个状态

一个国家
类项目{
id:编号;
名称:字符串;
位置:“设备”|“杂志”|“商店”//|等未来。。。
}
类AppState{
项目:项目[];
}
常量getItems=(存储:存储):可观察=>{
返回存储。选择((appStore:appStore)=>appStore.items);
}
const getItemsInEquipment=(存储:存储):可观察=>{
const items$:Observable=getItems(store);
返回项目$.pipe(
映射(items=>items.filter(item=>item.location==“设备”))
);
}
const getItemsInMagazine=(存储:存储):可观察=>{
const items$:Observable=getItems(store);
返回项目$.pipe(
映射(items=>items.filter(item=>item.location==“杂志”))
);
}
const getItemsInShop=(存储:存储):可观察=>{
const items$:Observable=getItems(store);
返回项目$.pipe(
映射(items=>items.filter(item=>item.location==“shop”))
);
}
或者

多国
类项目{
id:编号;
名称:字符串;
}
类AppState{
项目设备:项目[];
项目杂志:项目[];
商品商店:商品[];
//|以及未来的其他。。。
}
const getItemsInEquipment=(存储:存储):可观察=>{
返回存储。选择((appStore:appStore)=>appStore.itemsInEquipment);
}
const getItemsInMagazine=(存储:存储):可观察=>{
返回存储区。选择((appStore:appStore)=>appStore.itemsInMagazine);
}
const getItemsInShop=(存储:存储):可观察=>{
返回存储。选择((appStore:appStore)=>appStore.itemsInShop);
}
常量getItems=(存储:存储):可观察=>{
const itemsInEquipment$:Observable=getItemsInEquipment(store);
const itemsInMagazine$:可观察=getItemsInMagazine(存储);
const itemsInShop$:Observable=getItemsInShop(store);
返回组合测试([itemsInEquipment$、itemsInMagazine$、itemsInShop$);
}

这两种方法的优缺点是什么?

我建议采用不同的方法。例如,在NgRx中,我们将为所有
设置单个实体状态,然后在每个列表中简单地存储
的ID数组。NGXS实验室有一个实体状态适配器,可能值得一试

基本上,所有
项都存储在一个对象中,其中键是每个
项的
id
字段:

项目实体:{
项目ID1:项目,
项目ID2:项目,
项目ID3:项目
}
这允许直接查找,并防止由阵列操作导致的性能损失


然后,您的每个
项设备
项存储
等都是简单的
ID数组
,允许您从实体状态获取它们。

我建议采用不同的方法。例如,在NgRx中,我们将为所有
设置单个实体状态,然后在每个列表中简单地存储
的ID数组。NGXS实验室有一个实体状态适配器,可能值得一试

基本上,所有
项都存储在一个对象中,其中键是每个
项的
id
字段:

项目实体:{
项目ID1:项目,
项目ID2:项目,
项目ID3:项目
}
这允许直接查找,并防止由阵列操作导致的性能损失

然后,每个
项目设备
项目存储
等都是
ID的简单数组
,允许您从实体状态获取它们