如何发送空动作? [英] How to dispatch an empty action?

查看:53
本文介绍了如何发送空动作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ngrx/effects .

我该如何调度一个空动作?

How can I dispatch an empty action?

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

This is how I am doing now:

 @Effect() foo$ = this.actions$
    .ofType(Actions.FOO)
    .withLatestFrom(this.store, (action, state) => ({ action, state }))
    .map(({ action, state }) => {
      if (state.foo.isCool) {
        return { type: Actions.BAR };
      } else {
        return { type: 'NOT_EXIST' };
      }
    });

由于必须返回操作,因此我正在使用return { type: 'NOT_EXIST' };.

Since I have to return an action, I am using return { type: 'NOT_EXIST' };.

是否有更好的方法可以做到这一点?

Is there a better way to do this?

推荐答案

我使用了类似的未知操作,但通常是在化简器的单元测试中使用.

I've used similar unknown actions, but usually in the context of unit tests for reducers.

如果您对效果中的相同操作不满意,则可以有条件地使用mergeMapObservable.of()Observable.empty()发出动作:

If you are uneasy about doing the same in an effect, you could conditionally emit an action using mergeMap, Observable.of() and Observable.empty() instead:

@Effect() foo$ = this.actions$
  .ofType(ChatActions.FOO)
  .withLatestFrom(this.store, (action, state) => ({ action, state }))
  .mergeMap(({ action, state }) => {
    if (state.foo.isCool) {
      return Observable.of({ type: Actions.BAR });
    } else {
      return Observable.empty();
    }
  });

这篇关于如何发送空动作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆