How to do if/else in ngrx/effects?

[亡魂溺海] 提交于 2019-12-07 02:36:52

问题


I am using ngrx/effects. I want to dispatch different actions based on a foo state in the store.

This is how I am doing now:

 @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 }}));

Is there a way to use RxJS 5 operators like partition, groupBy, if, case in this post? I cannot use it correctly now.


回答1:


Having 2 separate effect sources are fine. Otherwise make it simple:

@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 }};
        }
    });


来源:https://stackoverflow.com/questions/39299386/how-to-do-if-else-in-ngrx-effects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!