angular – 如何使用ngrx和effects订阅动作成功回调

前端之家收集整理的这篇文章主要介绍了angular – 如何使用ngrx和effects订阅动作成功回调前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试做一些简单的事情 – 在保存一些实体后(使用http请求)
我想导航回列表路线.问题是:如何订阅成功动作(或者可能是减速器或效果?)

这是我的行为代码

@H_301_4@static SAVE_POST = '[POST] Save POST'; savePOST(Post): Action { return { type: PostActions.SAVE_POST,payload: Post }; } static SAVE_POST_SUCCESS = '[POST] Save POST Success'; savePOSTSuccess(Post): Action { console.log('action: savePostSuccess') return { type: PostActions.SAVE_POST_SUCCESS,payload:Post }; }

我正在使用效果

@H_301_4@@Effect() savePost$= this.update$ .ofType(PostActions.SAVE_POST) .map(action => action.payload) .switchMap(post => this.svc.savePost(post)) .map(post => this.postActions.savePOSTSuccess(post));

减速器:

@H_301_4@const initialState: PostListState = []; export default function (state = initialState,action: Action): PostListState { switch (action.type) { case PostActions.LOAD_POST_SUCCESS: { return action.payload; } case PostActions.SAVE_POST_SUCCESS: { console.log('SavePOST SUCCESS',action.payload) let index = _.findIndex(state,{_id: action.payload._id}); if (index >= 0) { return [ ...state.slice(0,index),action.payload,...state.slice(index + 1) ]; } return state; } default: { return state; } } }

在我的组件中,我想订阅成功回调:

@H_301_4@handlePostUpdated($event) { this.post = this.code; let _post: Post = Object.assign({},{ _id: this.id,name: this.name,text: this.post }); this.store.dispatch(this.postActions.savePOST(_post)); //not have "subscribe" method }

感谢帮助

您也可以订阅组件中的操作: @H_301_4@[...] import { Actions } from '@ngrx/effects'; [...] @Component(...) class SomeComponent implements OnDestroy { destroyed$= new Subject<boolean>(); constructor(updates$: Actions) { updates$ .ofType(PostActions.SAVE_POST_SUCCESS) .takeUntil(this.destroyed$) .do(() => /* hooray,success,show notification alert ect.. */) .subscribe(); } ngOnDestroy() { this.destroyed$.next(true); this.destroyed$.complete(); } }

猜你在找的Angularjs相关文章