我正在使用 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/