我幾乎是一個redux
模式的新手,並剛剛開始使用ngrx
。這真棒,這是我想盡可能使用的東西,但我有幾個關於Store
概念的問題。商店vs商店<T>
我會嘗試通過幾個示例來描述問題,並在本帖末尾提問我的問題。
讓我們先從AppState
接口和減速器:
export interface AppState{
people: Person[],
events: Event[]
}
//events reducer
export function eventsReducer(state: any = {}, {type, payload}): Event[]{
switch(type){
case "ADD_EVENT":
return [...state, payload];
default:
return state;
}
}
//people reducer
export function peopleReducer(state: any = {}, {type, payload}): Person[]{
switch(type){
case "ADD_PERSON":
return [...state, payload];
default:
return state;
}
}
//root reducer
const root: ActionReducer<AppState> = combineReducers({people: peopleReducer, events: eventsReducer});
const INITIAL_STATE = {
people:[],
events: []
}
export function rootReducer(state: any = INITIAL_STATE, action: any){
return root(state, action);
}
rootReducer
加入這樣的:
//part of the AppModule
...
imports:[
...,
StoreModule.provideStore(rootReducer)
]
,並在主AppComponent
這裏是如何我accesing的store
:
//part of the AppComponent
export class AppComponent{
people: Observable<Person[]>;
events: Observable<Event[]>;
constructor(private store: Store<AppState>){
this.people = store.select('people');
this.events = store.select('events');
}
}
現在,一切我很喜歡這個概念,但我注意到,如果我從AppState
界面刪除其中一個屬性(例如,我刪除了people
屬性,其他所有內容保持不變),則沒有任何更改(或中斷)。
所以我想知道有Store<AppState>
,而不是僅僅Store
主要的原因是什麼,什麼是使用Store<AppState>
(它實際上取得對抗只是用Store
差異)的主要優勢是什麼?另外,是否有一種方法可以在AppState更改時至少執行運行時錯誤,但其他所有內容都保持不變?
我使用錯誤的可能性也非常高,但我仍然想知道這些問題的答案。
感謝您的回答。我一直在研究'Store'實現中''T'的功能,但從來沒有打算檢查'select'方法。 –
eminlala