angular - NgRx 发送了一个无效的 Action

标签 angular typescript ngrx ngrx-effects

这里的问题主要是因为ofType()。我有一个简单的操作,用户正在分派(dispatch)一个类型,然后将请求发送到后端,作为响应,它将返回一个对象数组,该对象将作为新的分派(dispatch)操作传递。问题是,对于新版本的 ngrx,无法指定 actions$ 的 ofType(),因为首先我们需要使用 pipe(),因此 map 不会以一种类型处理...

这是我的实现:

@Effect()
    getGrades = this.actions$
    .pipe(
      ofType(StudentActions.TRY_SET_SUBJECTS),
      mergeMap((action: StudentActions.TrySetSubjects) => {
        const id = action.payload.id;
        return this.httpClient.get(`http://localhost:8080/api/v1/student/${id}/grades`)

      }),
      map((response: any[])  => {
        console.log('StudentEffects -> getGrades')
        const subjects: Subject[] = response

        return [
          new StudentActions.SetSubjects(subjects)
        ]
      })
    );

以下是它在以前版本中的工作方式:

@Effect()
    getGrades = this.actions$
    .ofType(StudentActions.TRY_SET_SUBJECTS)
    .pipe(
      mergeMap((action: StudentActions.TrySetSubjects) => {
        const id = action.payload.id;
        return this.httpClient.get(`http://localhost:8080/api/v1/student/${id}/grades`)

      }),
      map((response: any[])  => {
        console.log('StudentEffects -> getGrades')

        const subjects: Subject[] = response

        return [
          new StudentActions.SetSubjects(subjects)
        ]
      })
    );

如何使用新版本处理响应并发送新的 StudentActions.SetSubjects(subjects)?

任何提示都会有所帮助,在此先感谢您。

编辑

实际上 map 有问题。起初我使用了 mergeMap 然后只是映射。我已将其更改为 mergeMap 和它们 concatMap。不确定 concatMap 是否是不错的选择,但它会等到之前的订阅结束,所以这听起来是个不错的选择。一切正常。

最佳答案

这是您要找的吗?

enter image description here

关于angular - NgRx 发送了一个无效的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55332130/

相关文章:

javascript - Angular2 动态样式表元素

Angular 2 : Custom validation for file extensions

Angular 路由器 : Child route can be called without parent url

javascript - 阅读更多 阅读更少 陷入 Angular

typescript 抽象类静态方法未强制执行

Angular 2 Material 2单选按钮默认选择

angular - 既然 Angular 是双向数据绑定(bind),为什么我应该在 Angular 中使用 Redux?

angular - 农业网格 : show dropdown on cell of grid with angular

javascript - Angular 等待变量初始化

javascript - ngrx : how the reducers function are invoked, 何时被调用?