私はかなり初心者で、redux
パターンで、ngrx
を使い始めました。素晴らしいですが、できるだけ多く使用したいものですが、Store
のコンセプトに関するいくつかの質問があります。Store vs Store <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
には、私はstore
をaccesingてる方法です:
//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
インターフェイスからプロパティの1つを削除すると(たとえば、people
のプロパティを削除しても、それ以外のものはすべて同じままです)、何も変わらない(または中断する)ことに気付きました。
だから私はStore<AppState>
を持つだけではなくStore
と(それが実際にはStore
を使っに対する違いを作られているところ)Store<AppState>
を使用する主な利点は何のための主な理由が何であるかを知っていただきたいと思いますか?また、AppStateが変更されたときに少なくともランタイムエラーを強制する方法はありますか?それ以外はすべて同じままですか?
私が間違って使っている可能性も非常に高いですが、私はまだこれらの質問に対する回答を知りたいと思います。
この回答に感謝します。私は、 'Store'実装で 'T 'を使って何をしているのか調べてきましたが、決して' select'メソッドを確認することには悩まされませんでした。 –
eminlala