我正在将@ngrx/effects从1.x更新到2.x
在1.x中,我可以访问状态树:
constructor(private updates$: StateUpdates<AppState>) {}
@Effect() bar$ = this.updates$
.whenAction(Actions.FOO)
.map(obj => obj.state.user.isCool)
.distinctUntilChanged()
.filter(x => x)
.map(() => ({ type: Actions.BAR }));
现在在2.x中,它只给我 Action 。还有没有办法访问状态树?还是我应该避免这样使用,因为这不是一个好习惯?
constructor(private actions$: Actions) {}
@Effect() bar$ = this.actions$
.ofType(ActionTypes.FOO)
.map((obj: any) => {
console.log(obj); // here is action only
return obj.state.user.isCool // so it is wrong here
})
.distinctUntilChanged()
.filter(x => x)
.map(() => ({ type: ActionTypes.BAR }));
最佳答案
另一种方法是使用.withLatestFrom(this.store)
。因此完整的代码是:
constructor(
private actions$: Actions,
private store: Store<AppState>
) {}
@Effect() bar$ = this.actions$
.ofType(ActionTypes.FOO)
.withLatestFrom(this.store, (action, state) => state.user.isCool)
.distinctUntilChanged()
.filter(x => x)
.map(() => ({ type: ActionTypes.BAR }));
关于angular - 如何获得对状态树的访问? (@ ngrx/效果2.x),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39565811/