如何在ngrx / effects中执行if / else?

时间:2016-09-02 19:17:55

标签: angular typescript ngrx

我正在使用ngrx / effects。我想根据商店中的foo状态发送不同的操作。

这就是我现在正在做的事情:

 @Effect() foo1$ = this.updates$
    .whenAction(Actions.FOO)
    .filter(obj => !obj.state.product.foo)
    .map<string>(toPayload)
    .map(x => ({ type: Actions.BAR1, payload: { x }}));

  @Effect() foo2$ = this.updates$
    .whenAction(Actions.FOO)
    .filter(obj => obj.state.product.foo)
    .map<string>(toPayload)
    .map(x => ({ type: Actions.BAR2, payload: { x }}));

有没有办法在this post中使用partitiongroupByifcase等RxJS 5运算符?我现在无法正确使用它。

1 个答案:

答案 0 :(得分:6)

有两个独立的效果源是好的。否则简单说明:

@Effect() foo$ = this.updates$
    .whenAction(Actions.FOO)
    .map(({action, state}) => {
        if (state.product.foo) {
            return { type: Actions.CHAT_GET_MESSAGES2, payload: { action.payload }};
        } else {
            return { type: Actions.CHAT_GET_MESSAGES, payload: { action.payload }};
        }
    });