Angular 在ngrx选择器中使用管道
我想要一个记忆选择器,其中包括一个rxjs转换在其投影仪功能。尝试这样获得每组Angular 在ngrx选择器中使用管道,angular,typescript,rxjs,ngrx,Angular,Typescript,Rxjs,Ngrx,我想要一个记忆选择器,其中包括一个rxjs转换在其投影仪功能。尝试这样获得每组FunctionProps的FunctionProps数组。由记忆选择器选择的FunctionProps[]数组的类别: export const selectGroupedFunctionProperties = createSelector( selectFunctionProperties, (f) => from(f).pipe( groupBy((v: Functi
FunctionProps的FunctionProps数组。由记忆选择器选择的FunctionProps[]
数组的类别:
export const selectGroupedFunctionProperties = createSelector(
selectFunctionProperties,
(f) => from(f).pipe(
groupBy((v: FunctionProps) => v.Category),
mergeMap(group => group.pipe(toArray())),
toArray()
)
);
如您所见,我使用“from”从源FunctionProps[]数组中获取了一个可管道观测值。这当然不是一个好主意,因为selectGroupedFunctionProperties选择器现在是消费组件中可观察的可观察的
是否有一种方法可以将管道放在记忆选择器中,而不是在组件this.store.select(selectFunctionProperties).pipe(…)
中使用管道
我希望能够组合选择器,使rxjs代码远离组件
否则,在选择器中使用管道是否有效
(因此,我无法避免将类型为script的数组的groupBy实现为选择器中的纯函数)
编辑
我直接使用管道操作符更改了选择器:
export const selectGroupedFunctionProperties = pipe(
select(selectFunctionProperties),
concatMap(functions => functions),
groupBy(v => v.Category),
mergeMap(group => group.pipe(toArray())),
toArray(),
);
在组件中,我接收到带有管道的Functionprops[][]阵列,而不是选择:
groupedFunctions$ = this.store.pipe(selectGroupedFunctionProperties);
您可以选择单独导出管道(不带createSelector):
请看一下这些文档:谢谢Gunnar,不知何故,我一直在阅读ngrx文档的“高级”部分。我就是这样尝试的。需要更多的帮助,groupBy的rxjs语句是不正确的(请参见编辑)明白了,必须在组件中使用管道而不是select。
export const selectGroupedFunctionProperties = pipe(
select(selectFunctionProperties),
groupBy((v: FunctionProps) => v.Category),
mergeMap(group => group.pipe(toArray()))
// I'm not quite sure, but I don't think you need the 'toArray'-operator
);