angular - 为什么 ngrx/redux 效果必须返回 Action ?使用像 elm 这样的 noop Action 是否被认为是不好的做法?

标签 angular redux ngrx ngrx-effects ngrx-store

我正在使用带有 Angular 和 ngrx/store 和 ngrx/effects 的 redux 风格的状态管理设计。每当我不从效果返回操作时,我都会收到错误消息:

Cannot read property 'type' of undefined

我研究了这个问题,发现在 elm 架构中有一个称为“noop”的 Action ,当您不想将另一个 Action 与您的效果链接时,您可以调用它什么都不做。到处调用这个 noop Action 对我来说似乎非常重复。我想知道这是否是一个不好的做法。有没有理由不能产生不返回 Action 的效果?效果的意图是否总是让 1 个 Action 触发另一个 Action ?我想知道我是否误解了如何使用效果。

谢谢!

最佳答案

默认情况下,ngrx/effect 会调度一个 Action 。

如果你想要一个效果是“即发即弃”的,你需要做的就是将 {dispatch: false} 作为参数添加到 @Effects() 装饰器。

来自@ngrx/effects docs :

Observables decorated with the @Effect() decorator are expected to be a stream of actions to be dispatched. Pass { dispatch: false } to the decorator to prevent actions from being dispatched.

Usage:

class MyEffects {
  constructor(private actions$: Actions) { }
    
  @Effect({ dispatch: false }) logActions$ = this.actions$
    .do(action => {
      console.log(action);
    });
}

在引擎盖下,这是通过 ignoreElements operator 实现的. (Here 是 ngrx/effects 的源代码,如果您有兴趣的话)。 ignoreElements 有一个 built in noop function得到called每次效果运行时。

简而言之,ngrx/effects 中不需要显式的 noop-action。我不会直接称其为发送 noop-action 的“不良做法”,但在使用 ngrx 时肯定没有必要。

关于angular - 为什么 ngrx/redux 效果必须返回 Action ?使用像 elm 这样的 noop Action 是否被认为是不好的做法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055379/

相关文章:

angular - 错误 ngx-daterangepicker-material 无法在环境上下文中声明访问器

javascript - Angular 2 RC1 - 找不到默认 socket

javascript - 在 redux 中间件中调度多个操作

angular - 服务循环的 ngrx 效果

javascript - 管道内的映射被忽略并且不会触发http调用

angular - ngrx 商店选择订阅仅针对特定操作

Angular 2 : ElementRef nativeElement vs querySelector performance

angular - @import styles in angular components styles with::ng-deep

javascript - Redux 状态正在随着 lodash 的 mergeWith 发生变化

javascript - 更新数组中的 1 个元素会触发 React 中其他元素的渲染吗?