how to subscribe to action success callback using ngrx and effects

后端 未结 6 1501
慢半拍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条回答
  •  再見小時候
    2020-12-07 18:27

    You can subscribe to actions in components as well:

    [...]
    import { Actions } from '@ngrx/effects';
    [...]
    
    @Component(...)
    class SomeComponent implements OnDestroy {
        destroyed$ = new Subject();
    
        constructor(updates$: Actions) {
            updates$.pipe(
               ofType(PostActions.SAVE_POST_SUCCESS),
               takeUntil(this.destroyed$)
            )
            .subscribe(() => {
               /* hooray, success, show notification alert etc.. */
            });
        }
    
        ngOnDestroy() {
            this.destroyed$.next(true);
            this.destroyed$.complete();
        }
    }
    

提交回复
热议问题