how to subscribe to action success callback using ngrx and effects

后端 未结 6 1496
慢半拍i
慢半拍i 2020-12-07 17:43

I\'m trying to do simple thing - after some entity saved (using http request) I want to navigate back to list route. The problem is : how to subscribe to success action (or

6条回答
  •  萌比男神i
    2020-12-07 18:33

    Based on this:https://netbasal.com/listening-for-actions-in-ngrx-store-a699206d2210 with some small modifications, as since ngrx 4 there is no Dispatcher anymore, but instead ActionsSubject:

    import { ActionsSubject } from '@ngrx/store';
    import { ofType } from "@ngrx/effects";
    
    subsc = new Subscription();
    
    constructor(private actionsSubj: ActionsSubject, ....) { 
      this.subsc = actionsSubject.pipe(
         ofType('SAVE_POST_SUCCESS')
      ).subscribe(data => {
         // do something...
      });
    }
    
    ngOnDestroy() {
      this.subsc.unsubscribe();
    }
    

提交回复
热议问题