我正在查看example application provided by NgRx的代碼。我注意到示例應用程序中的每個reducer函數都有一個返回值,該值由該特定reducer的State
接口鍵入。例如,書籍減速機具有下面的代碼:State vs ActionReducer <State> as NgRx reducer返回類型
export interface State {
ids: string[];
entities: { [id: string]: Book };
selectedBookId: string | null;
}
export const initialState: State = {
ids: [],
entities: {},
selectedBookId: null,
};
export function reducer(
state = initialState,
action: book.Actions | collection.Actions
): State {
後來,我正在讀一本關於NGRX由奧倫Farhi題爲Reactive Programming with Angular and NgRx,和整個的代碼片段來呈現出減速功能的常見車身結構( 。第24-25頁)爲共同的結構的代碼顯示減速函數的返回值作爲由ActionReducer
正在鍵入與State
作爲類型參數(在這種情況下稱爲SomeInterface
而非State
):
export interface SomeInterface {
items: Item[],
filter: string
}
let initialState: SomeInterface = {
items: [],
filter: ''
}
export function MyReducer (
state: SomeInterface = initialState,
action: Action
): ActionReducer<SomeInterface> {
爲什麼一個代碼示例使用狀態和其他u將ActionReducer與State作爲Reducer函數的返回值的類型參數?爲什麼會選擇其中一個功能簽名?每個服務的目的是什麼?
本書是爲NgRx 2.2.1編寫的,而示例應用程序是針對最新版本的NgRx(版本4.1.1)。我猜測返回類型的區別不能簡單地由NgRx的差異來解釋版本,因爲最新版本的NgRx也有ActionReducer。
謝謝!
使用此[* *中博客**](https://medium.com/@aravindfz/setting-up-storemodule-in-ngrx-4-0-b7c60732aa64)設置爲ngrx-4.0 – Aravind