轉換可以進入效果器或減速器。
如果有任何驗證需要執行,我會把它放在效果中 - 我可以選擇分派錯誤操作。否則,我會把它放入reducer中,因爲這是我通常將動作有效載荷轉換爲狀態的地方。
還有另一種選擇:您可以使用選擇器。也就是說,消息可以作爲一個簡單的數組存儲在狀態中,並且選擇器可以用來轉換狀態的消息,將它們按對方分組 - 或者其他任何東西。如果我有多種分組消息的方式,這是我選擇的選項。
的@ngrx/example-app
包含selectors一些例子:
/**
* A selector function is a map function factory. We pass it parameters and it
* returns a function that maps from the larger state tree into a smaller
* piece of state. This selector simply selects the `books` state.
*
* Selectors are used with the `select` operator.
*
* ```ts
* class MyComponent {
* constructor(state$: Observable<State>) {
* this.booksState$ = state$.select(getBooksState);
* }
* }
* ```
*/
export const getBooksState = (state: State) => state.books
謝謝。這使得它更清晰。 – balteo
增加了另一個選項:選擇器。 – cartant