效果ngrx v8中的链式动作

问题描述 投票:0回答:1

我想使用ngrx的最新语法链接效果。我已经搜索并找到关于stackoverflow的this问题,但是它具有旧语法。

这是当前效果:

export class DeleteCommentEffect {

deleteComment$ = createEffect(() =>
    this.actions$.pipe(
        ofType(DeletingComment),
        mergeMap((action) => this.commentService.deleteComment(action.dossierId, action.commentId)
            .pipe(
                map((statusCode: number) => {
                    return DeleteCommentSuccess({ statusCode });
                }),
                catchError((error: HttpErrorResponse) => {
                    return of(DeleteCommentError({ error }));
                })
        ))
    )
);

constructor(
    private actions$: Actions,
    private commentService: DBCommentService) {
}

}

我想在成功删除评论后链接此效果。

export class GetCommentEffects {
getComment$ = createEffect(() =>
    this.actions$.pipe(
        ofType(GettingComment),
        mergeMap(action =>
            this.commentService.getAllComments(action.dossierId).pipe(
                map((comments: Comment[]) => {
                    return GetCommentSuccess({comments});
                }),
                catchError((error: HttpErrorResponse) => {
                    return of(GetCommentError({error}));
                })
            ))
    )
);

constructor(
    private actions$: Actions,
    private commentService: DBCommentService
) {}

}

我已经搜索了ngrx docs,但似乎没有提及如何链接效果。

ngrx-effects
1个回答
0
投票

[创建一个监听分派DeleteCommentSuccessGettingComment动作的效果。

deleteCommentSuccess$ = createEffect(() =>
  this.actions$.pipe(
    ofType(DeleteCommentSuccess),
    map(() => CommentActions.GettingComments())
  )
);
© www.soinside.com 2019 - 2024. All rights reserved.