我正在使用Angular 17和ngrx.

我有以下可以正常工作的 Select 器:

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")

reducedPhotos是产品[]

推荐答案

操作后返回值

function xboxPhotos(key: string, array: any) {
  return array.reduce((all: Product[], current: Product) => {
    if (current.platform == key) {
      all.push(current);
    }
    return all; // <- changed here
  }, []) as Product[]
}

Angular相关问答推荐

元件不工作时的Angular 17属性 Select 器

如何使用解析器处理Angular 路由中存储的查询参数以避免任何额外的导航?

PrimeNg选项卡视图选项卡

Ngrx Select 器返回部分对象

VS 2022 停留在运行 Angular 应用程序上

NX如何在前端和后端使用一个接口库

PrimeNG:如何以编程方式更改分页器中的选定页面?

Angular6:RxJS switchMap 运算符未按预期工作

Angular:组件的内容投影访问数据

Angular:显示带有嵌套子项的内容投影

在ionic 5中获取url传递的参数

RxJS - .withLatestFrom 的多个来源

使用 rxjs 处理刷新令牌

在 Ionic 2 中使用图像资源的正确方法

angular4应用程序中输入的最小值和最大值

如何组合两个可观察到的结果?

Angular 5 中 value 和 ngValue 的区别

从 angular2 模板调用静态函数

在 Angular 组件模板中添加脚本标签

如何在 angular2 中的 div 的 contenteditable 上使用 [(ngModel)]?