我正在分派两个动作,不需要顺序执行。我尝试了以下代码:
@Effect()
loadIntResponse$ =
this.actions.ofType(fromActions.LOAD_INT_RESPONSE).pipe(
switchMap(() => {
return this.Service.int().pipe(
tap(x => this.store.dispatch(new fromActions.LoadService(x))),
map(responseResult => {
return new fromActions.LoadIntResponseSuccess(responseResult)
}),
catchError(error => {
let err: responseResult = {
status: "FALSE",
message: error
}
return of(new fromActions.LoadIntResponseFail(err))
})
)
})
)
loadService$ = this.actions.ofType(fromActions.Load_Service).pipe(
switchMap((action: fromActions.LoadService) => {
if (action.payload.status == 'TRUE' {
return this.reuseService.reuseServiceCall().pipe(
map( result ==> {
return of({type: fromActions.LOAD_SERVICE_SUCCESS, payload:
result})
}),
catchError(error => {
let err: responseResult = {
status: "FALSE",
message: error
}
return of(new fromActions.LoadServiceFail(err))
})
)
})
)
但是它不会触发其他效果LoadService()
预期结果:触发另一个效果LoadService()
LoadService接受任何类型参数。
我非常感谢您的帮助。谢谢!
答案 0 :(得分:1)
我进行了一些修改,以匹配您在评论中想要的内容。首先,您有一个效果,调用int()
,然后如果成功则将调度LoadIntResponseSuccess
,否则将调度到catchError
。您还有第二种效果,可以监听LoadIntResponseSuccess
,并在触发LoadService
后立即调度LoadIntResponseSuccess
。
第二种效果可能是错误的,因为我不知道您的操作如何传递数据的格式,因此x
可能是x.something
loadService效果现在首先过滤有效负载。 switchMap
中不能包含if来返回任何内容或不返回任何内容,如果要if则必须具有else!过滤之后,将调用服务,如果服务成功,则分派LoadServiceSuccess
,否则,服务将转到catchError
。
@Effect()
loadIntResponse$ =
this.actions.ofType(fromActions.LOAD_INT_RESPONSE).pipe(
switchMap(() => this.Service.int()),
map((responseResult) => new fromActions.LoadIntResponseSuccess(responseResult)),
catchError((error: any, effects: Observable<Action>) => {
let err: responseResult = {
status: "FALSE",
message: error
}
return effect.pipe(startWith(new fromActions.LoadIntResponseFail(err)))
})
)
@Effect()
loadIntResponseSuccess$ =
this.actions.ofType(fromActions.LOAD_INT_RESPONSE_SUCCESS).pipe(
map((action) => new fromActions.LoadService(action.payload))
)
@Effect()
loadService$ = this.actions.ofType(fromActions.Load_Service).pipe(
switchMap((action) => { if (action.payload.status === 'TRUE') {
return [new fromActions.LoadServiceSuccess(action)]
} else {
return [new fromActions.LoadServiceFail()]
} ),
catchError((error: any, effects: Observable<Action>) => {
let err: responseResult = {
status: "FALSE",
message: error
}
return effect.pipe(startWith(new
fromActions.LoadServiceFail(err)))
})
)