您可以创建自己的操作类型,该类型具有payload
定义,检查示例应用程序 https://github.com/ngrx/platform/blob/48a2381c212d5dd3fa2b9435776c1aaa60734235/example-app/app/books/actions/collection.ts#L17以供参考:
class AddBookAction implements Action {
readonly type = ADD_BOOK;
constructor(public payload: Book) {}
}
然后使用该类型减速机 https://github.com/ngrx/platform/blob/48a2381c212d5dd3fa2b9435776c1aaa60734235/example-app/app/books/reducers/collection.ts#L15:
function reducer(state = initialState, action: AddBookAction): State
可以像这样调度动作this https://github.com/ngrx/platform/blob/48a2381c212d5dd3fa2b9435776c1aaa60734235/example-app/app/books/containers/selected-book-page.ts#L33:
this.store.dispatch(new AddBookAction(book));
另请注意,示例应用程序combines https://github.com/ngrx/platform/blob/48a2381c212d5dd3fa2b9435776c1aaa60734235/example-app/app/books/actions/collection.ts#L75减速器可以采用单个联合类型的所有操作类型:
export type Actions =
| AddBookAction
| AddBookSuccessAction
export function reducer(state = initialState, action: Actions): State