Angular ngrx/store 4:从store中选择时获得意外的子状态

Angular ngrx/store 4:从store中选择时获得意外的子状态,angular,ngrx-store-4.0,Angular,Ngrx Store 4.0,我有几家分店。假设Sub1和Sub2具有以下状态和减缩器(操作对我的问题不感兴趣……我猜): Sub1: export interface Sub1State { sub1value: string } export function sub1reducer(state: Sub1State, action: Action): Sub1State { ... } export interface Sub2State { sub2value: number } export fu

我有几家分店。假设
Sub1
Sub2
具有以下状态和减缩器(操作对我的问题不感兴趣……我猜):

Sub1:

export interface Sub1State {
  sub1value: string
}

export function sub1reducer(state: Sub1State, action: Action): Sub1State {
  ...
}
export interface Sub2State {
  sub2value: number
}

export function sub2reducer(state: Sub2State, action: Action): Sub2State {

}
Sub2:

export interface Sub1State {
  sub1value: string
}

export function sub1reducer(state: Sub1State, action: Action): Sub1State {
  ...
}
export interface Sub2State {
  sub2value: number
}

export function sub2reducer(state: Sub2State, action: Action): Sub2State {

}

然后我将这两个子状态组合成一个
ActionReducerMap

当您获得应用程序的状态时,它返回一个对象。您所在的州有两个子州,分别称为sub1和sub2。每一个都是它自己的目标。因此,它正在按预期工作。您需要执行“返回state.sub1.sub1value”

或者我通常做的是,通过这样做订阅特定的reducer,然后我只返回特定的子状态,而不是整个状态:

constructor(private store: Store<Sub1State>) {
  store.select('sub1').subscribe((state : Sub1State) => {
    console.log(state);
    this.sub1value$ = state.sub1value;
  });
}
构造函数(私有存储:存储){
store.select('sub1')。subscribe((状态:Sub1State)=>{
console.log(状态);
this.sub1value$=state.sub1value;
});
}

那么选择器功能是如何正常工作的呢?在大多数示例中,我看到了一个签名,如
getValue1(state:Sub1State):string
。但是如果我将此选择器函数传递给
store.select(getValue1)
它将不起作用,因为传递给选择器函数的对象不是
Sub1State
类型,而是带有
sub1
根元素。同时,还注入子状态的存储
constructor(private store:store)
。如果按照我所描述的操作,您将返回子1状态。执行存储。选择('sub1')。订阅(…)。这将给你一个sub1状态对象好的,对的,这是有效的。但如果我这样做的话,我也可以编写一个简单的服务,在那里我必须手动订阅和取消订阅。我失去了Flux/Redux/Store架构的一些很酷的特性。但我不想手动订阅和取消订阅,我只想获得应用程序状态的一部分,并将其保存在一个本地可观察文件中。下面是一个我期望有效但不起作用的示例:
{
  sub1value: 'initial value'
}
{
  sub1: {
    sub1value: 'initial value'
  }
}
constructor(private store: Store<Sub1State>) {
  store.select('sub1').subscribe((state : Sub1State) => {
    console.log(state);
    this.sub1value$ = state.sub1value;
  });
}