我正在使用 Angular 17 和 ngrx。
我有以下工作正常的选择器:
export const getPhotoState = createFeatureSelector<ProductResponse>('selectedPhotos');
export const selectPhotos = createSelector(getPhotoState, (state: ProductResponse) => { return state.results });
export const PhotoCount = createSelector(getPhotoState, (state: ProductResponse) => { return state.item_Count });
我不想编写另一个效果/减速器来发送到服务器,因为我只想获取已返回的数据的子集。
我写了以下内容:
export const XboxPhotos = createSelector(selectPhotos, (reducedPhotos) => {
return xboxPhotos('xbox', reducedPhotos)
})
function xboxPhotos(key: string, array: any) {
return array.reduce((all: Product[], current: Product) => {
if (current.platform == key) {
all.push(current);
}
}, []) as Product[]
}
我收到以下错误:
无法读取未定义的属性(读取“push”)
reducePhotos 是产品[]
返回运算后的值
您正在修改累加器(在本例中为数组
all
),但从未返回它。如果您return all
在减速器回调结束时,您将不会再看到此错误。