@ngrx Effect does not run the second time

前端 未结 2 1106
栀梦
栀梦 2020-12-11 02:22

I\'ve just started learning about @ngrx/store and @ngrx.effects and have created my first effect in my Angular/Ionic app. It runs ok the first time but if I dispatch the eve

2条回答
  •  盖世英雄少女心
    2020-12-11 03:28

    You must move map() and catchError() into swithchMap() as following

    @Effect()
    public event_response$ = this.action$.pipe(
        ofType(SEND_EVENT_RESPONSE_ACTION),
        switchMap((payload) => {
            return this.myService.eventResponse(payload.eventId,payload.response).pipe(
                map((data: DataType) => new SentEventResponseAction(data)),
                catchError((error) => Observable.of(new ErrorOccurredAction(error)))
            })
        );
     );
    

    Please note that, evetResponse() method inside myService should return an observable in order to use pipe afterward. In case your method inside service returns Promise, you can convert it into an observable by the use of from in the rxjs package as below:

    import { from } from 'rxjs';
    ...
    const promise = this.myService.eventResponse(payload.eventId,payload.response);
    const observable = from(promise);
    return observable.pipe(...
    

    For more and detail description take a look at this link

提交回复
热议问题