@ngrx/effects 再次派发动作

@ngrx/effects dispatch action again

我是 ngrx/store 和效果方面的新手,所以我仍然不了解调度操作的流程:

  1. actions -> effect -> reducer -> store
  2. actions -> reducer -> effect -> reducer -> store

我的问题是,当我调用 store.select() 时,我发现该操作被分派了两次,这就是我为测试所做的:

Reducer function

switch (action.type) { 
  ...
  default: { 
    console.log('In reducer function'); 
    return state; 
  }
}

Effects class constructor

constructor( private action$: Actions ) { 
  console.log('in effect constructor'); 
}

and this what I get in the console

In reducer function
in effect constructor
In reducer function

尝试不仅登录 reducer 一条消息,还登录 action.type 然后你会看到它有 2 个不同的操作:store initeffects init.

当你派遣一个 action 时,它首先进入 effects,然后进入 reducerreducer 可以更新相关的 state .

通常人们会发送加载动作,它在效果中处理,效果加载数据和 returns 带有有效负载的成功动作,然后减速器接收成功动作并将有效负载设置为其状态。