这是我今天与ngrx / store有关的第二个问题:(
我在Angular 5项目中使用ngrx / store。我存储的应用程序状态看起来像这样(仅供参考)
class AppState{
private customerList: Customer [];
private selectedCustomer: Customer;
private countriesOperational: Country [];
}
我为状态对象创建了一个reduce,如下所示:
export function CustomerReducer(state: AppState = new AppState(), action: Action
){
switch(action.type){
case 'updateSelectedCustomer':
return {...state, selectedCustomer:action.payload}
//other cases here
default:
return state;
}
}
我的模块具有以下内容:
StoreModule.forRoot({applicationState:CustomerReducer});
在组件中,当我执行store.select(state => state)
时,获得的对象被包装在内部对象applicationState中(来自forRoot)。
所以我无法使用强类型值,因为applicationState不是AppState的一部分。它仅由减速器返回。我必须做这样的事情:
let state$:Observable<any> = store.select(state => state);
state$.subscribe(newState => this.currentState = newState.applicationState);
引用了同一问题here:
有没有办法在这里使用强类型值而不是any
。例如应该是这样的:
let state$:Observable<AppState> = store.select(state => state);
state$.subscribe(newState => this.currentState = newState);