如何连接减速器逻辑

时间:2019-10-05 20:11:30

标签: angular ngrx ngrx-store

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false}))
);

在上面的示例中,我在多个操作(isLogging属性)之间共享了逻辑。 但是,在分派loginSuccesfull操作时,将仅触发其中的一个reduce。

可以连接共享逻辑而不用编写:

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state, isLogging: false /*do sth other */})),
  on(loginFailed, (state) => ({...state, isLogging: false}))
);

1 个答案:

答案 0 :(得分:2)

(目前)这是不可能的,只有相同动作类型的最后一个注册动作会接收更新。

const authReducer = createReducer(
  initialState,
  on(startLogin, (state) => ({...state, isLogging: true})),
  on(loginSuccessful, (state) => ({...state /*do sth other */})),
  on(loginSuccessful, loginFailed, (state) => ({...state, isLogging: false})) // only this one will be triggered for loginSuccessful
);

我们刚刚合并了一个PR,以使您的用例成为可能,下一个版本可能会“淘汰”。