首页 > 解决方案 > ngrx on dispatching an action is it possible to not trigger the associated effect?

问题描述

I dispatch my ngrx action with:

store.dispatch(new MyAction(payload));

MyAction has an associated effect. When I dispatch the action the effect fires. Great!

This works as intended. But I only want the effect to fire sometimes. Can you do something like this?

store.dispatch(new MyAction(payload), {triggerEffect: false});

Or do I need to create a 2nd action that is handled exactly the same as MyAction by the reducers and simply not attach an effect to the 2nd Action? I'd rather not go down this route if I can avoid it. Thx

标签: javascriptangularngrx

解决方案


假设您只想在有效负载具有特定属性时触发效果。我会做这样的事情。

@Effect() myEffect$ = this.action$
    .ofType('ACTION_TYPE')
    .filter(action => action.payload.triggerSideEffect)
    .map( () => Observable.of({type: "DO_SIDE_EFFECT"}));

推荐阅读