我正在查看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