angular - 如何派发空 Action ?

标签 angular typescript rxjs5 ngrx

我正在使用 ngrx/effects .

我如何发送一个空 Action ?

我现在是这样的:

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

因为我必须返回一个 Action ,所以我使用 return { type: 'NOT_EXIST' };

有更好的方法吗?

最佳答案

选择的答案不再适用于 rxjs6。所以这是另一种方法。

虽然我更喜欢在另一个答案中描述的大多数情况下进行过滤,但有时使用 flatMap 会很方便,尤其是当你在做复杂的事情时,对于过滤函数来说太复杂了:

import { Injectable } from '@angular/core';
import { Actions, Effect, ofType } from '@ngrx/effects';
import { flatMap } from 'rxjs/operators';
import { EMPTY, of } from 'rxjs';

@Injectable()
export class SomeEffects {
  @Effect()
  someEffect$ = this._actions$.pipe(
    ofType(SomeActionTypes.Action),
    flatMap((action) => {
      if (action.payload.isNotFunny) {
        return of(new CryInMySleepAction());
      } else {
        return EMPTY;
      }
    }),
  );

  constructor(
    private _actions$: Actions,
  ) {
  }
}

关于angular - 如何派发空 Action ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39761826/

相关文章:

css - Angular HTML class.otherclass 解释

javascript - 如何使用 TypeScript 键入类似 jQuery 的初始化模式?

javascript - 使用 ts-node-dev 运行一个简单的 express 应用程序并得到错误 : False expression: Non-string value passed to `ts.resolveTypeReferenceDirective`

javascript - RxJS 函数签名

javascript - RxJS:如何导入 Just

angular - 如何使用 Keycloak 获取当前用户名?

javascript - 如何在 Angular 2 中按需延迟加载组件和模块

Angular 4 路由 : Is it possible to add constraints to route parameters?

typescript - RxJS 延迟至少 X 毫秒

javascript - 将 RxJS v4 代码转换为 v5,处理带有 "pull"的队列