这里的问题主要是因为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 是否是不错的选择,但它会等到之前的订阅结束,所以这听起来是个不错的选择。一切正常。
最佳答案
关于angular - NgRx 发送了一个无效的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55332130/