简体   繁体   中英

How to dispatch multiple actions from ngrx 7 effects

The idea is to listen for one action to trigger this effect and then make a service call that returns data. When the service call has been successful, I want to dispatch two different actions. I have tried many different combinations that I found on SO, but none of them work - only the first action gets dispatch, completely ignoring the second one. Tried this with switchMap , mergeMap but still only the first action gets dispatched.

@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()))
            )
        )
));

My app information:

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 version 7.3.0.

Try this:

@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()))
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM