如何从ngrx 7效果分派多个动作

时间:2019-03-23 13:10:20

标签: angular typescript ngrx ngrx-effects

这个想法是监听一个动作来触发这种效果,然后进行返回数据的服务调用。服务调用成功后,我想调度两个不同的操作。我尝试了在SO上找到的许多不同组合,但是它们都不起作用-只有第一个动作被调度,而完全忽略了第二个动作。与switchMapmergeMap进行了尝试,但仍然只分派了第一个动作。

@Effect()
    getAllShows$ = this.actions$
    .pipe(
        ofType(GetAllShowsAction.GET_ALL_SHOWS),
        switchMap((action: any) => this.showService.getAllShows(action.payload).pipe(
            mergeMap( response => { return [new GetAllShowsAction(action.payload + 1), new GetAllShowsSuccessAction(response)] },
            catchError( error => of(new GetAllShowsErrorAction()))
            )
        )
));

我的应用信息:

Angular CLI: 7.3.6
Node: 10.10.0
OS: darwin x64
Angular: 7.2.10
... animations, common, compiler, compiler-cli, core, forms
... language-service, platform-browser, platform-browser-dynamic
... router

Package                           Version
-----------------------------------------------------------
@angular-devkit/architect         0.13.6
@angular-devkit/build-angular     0.13.6
@angular-devkit/build-optimizer   0.13.6
@angular-devkit/build-webpack     0.13.6
@angular-devkit/core              7.3.6
@angular-devkit/schematics        7.3.6
@angular/cli                      7.3.6
@angular/fire                     5.1.2
@ngtools/webpack                  7.3.6
@schematics/angular               7.3.6
@schematics/update                0.13.6
rxjs                              6.4.0
typescript                        3.2.4
webpack                           4.29.0

NgRx版本7.3.0。

1 个答案:

答案 0 :(得分:2)

尝试一下:

@Effect()
    getAllShows$ = this.actions$
    .pipe(
        ofType(GetAllShowsAction.GET_ALL_SHOWS),
        switchMap((action: any) => this.showService.getAllShows(action.payload)),
        switchMap(res => [
            new GetAllShowsAction(action.payload + 1),
            new GetAllShowsSuccessAction(response)
        ]),
        catchError( error => of(new GetAllShowsErrorAction()))
);