ngrx effect not being called when action is dispatched from component

前端 未结 5 1829
情书的邮戳
情书的邮戳 2021-02-12 11:35

I am having an issue with the ngrx store not dispatching an action to the effect supposed to deal with it.

Here is the component that tries to dispatch:



        
5条回答
  •  借酒劲吻你
    2021-02-12 11:57

    Your store's stream may be stopping because of either unhandled errors or - perhaps more confusingly - errors that seem 'handled' using .catch that actually kill the stream without re-emitting a new Observable to keep things going.

    For example, this will kill the stream:

    this.actions$
        .ofType('FETCH')
        .map(a => a.payload)
        .switchMap(query => this.apiService.fetch$(query)
            .map(result => ({ type: 'SUCCESS', payload: result }))
            .catch(err => console.log(`oops: ${err}`))) // <- breaks stream!
    

    But this will keep things alive:

    this.actions$
        .ofType('FETCH')
        .map(a => a.payload)
        .switchMap(query => this.apiService.fetch$(query)
            .map(result => ({ type: 'SUCCESS', payload: result }))
            .catch(e => Observable.of({ type: 'FAIL', payload: e}))) // re-emit
    

    This is true for any rxjs Observable btw, which is especially important to consider when broadcasting to multiple observers (like ngrx store does internally using an internal Subject).

提交回复
热议问题