我有几个分店。让我们说Sub1
,并Sub2
使用以下状态和减速器(我的问题对我的问题不感兴趣......我猜):
子1:
export interface Sub1State {
sub1value: string
}
export function sub1reducer(state: Sub1State, action: Action): Sub1State {
...
}
子2:
export interface Sub2State {
sub2value: number
}
export function sub2reducer(state: Sub2State, action: Action): Sub2State {
}
然后我将这两个子状态合并为一个ActionReducerMap<ApplicationState>
:
export interface ApplicationState {
sub1: Sub1State,
sub2: Sub2State
}
export const reducers: ActionReducerMap<ApplicationState> = {
sub1: sub1reducer,
sub2: sub2reducer
};
并将其“注册”到商店:
StoreModule.forRoot(reducers)
现在,在 Angular 组件中,我想选择Sub1
商店并获取sub1value
字符串。所以我做...
sub1value$: Observable<string>;
constructor(private store: Store<Sub1State>) {
this.sub1value$ = store.map(state => {
console.log(state);
return state.sub1value;
});
}
在日志语句中,我希望得到以下对象(Sub1State 对象):
{
sub1value: 'initial value'
}
但我真正得到的是这个对象:
{
sub1: {
sub1value: 'initial value'
}
}
这是否按预期工作?如果是,我将如何使用接口 Sub1State?因为sub1
不是接口的一部分。
state.sub1value
而且很好奇的是,在调用明显错误的内容时,我没有收到任何错误(编译器错误和运行时错误),因为它必须是state.sub1.sub1value
. 我理解没有运行时错误,它只是未定义。但在我看来,TypeScript 编译器应该在这里抛出一个错误。
我真的很困惑:/
编辑
这是我期望它如何工作的示例:https ://stackblitz.com/edit/angular-w34kua?file=app%2Fapp.component.ts