Store.select bug?

时间:2016-07-30 22:18:22

标签: javascript typescript angular ngrx

Store.select应该得到一个字符串,告诉他我要观察的商店的属性,但问题是他没有这些属性。 Insead他将reducer函数作为暴露状态属性的属性。

https://github.com/ngrx/store很容易注意到出了问题。

他们的代码:

counter: Observable<number>;
constructor(public store: Store<AppState>){
     this.counter = store.select('counter');
}

我的代码:

export interface AppState{
  connectedAccountId:number;
}
@Injectable()
export class ConnectedAccountService {
  public connectedAccountId$:Observable<number>;
  constructor(private _store:Store<AppState>,private _accountService:AccountService)
  {
    this.connectedAccountId$ = this._store
      .select(state=>
      {
        console.log(state);
        let id:number=state.connectedAccountId; //x=undefined because state doesn't have 'connectedAccountId' property.
        return state.connectedAccountReducer.connectedAccountId; //this line is working!
        // Error:(35, 22) TS2339: Property 'connectedAccountReducer'
        // does not exist on type 'AppState'.
      });

    this.connectedAccountId$ = this._store.select("connectedAccountId");
    // Error:(37, 5) TS2322: Type 'Observable<{}>' is not
    // assignable to type 'Observable<number>'.
    // Type '{}' is not assignable to type 'number'.
  }

以下代码将起作用并执行大错误所需的操作:

this.connectedAccountId$ = this._store
      .select(state=>
      {
        return state.connectedAccountReducer.connectedAccountId; //this line is working!
        // Error:(35, 22) TS2339: Property 'connectedAccountReducer'
        // does not exist on type 'AppState'.
      });

为什么打字稿会抛出这些错误?我该如何解决?

1 个答案:

答案 0 :(得分:0)

我的助推器:

bootstrap(AppComponent, [
  AccountService,ConnectedAccountService,
  provideStore({connectedAccountReducer})
]);

更改为:

bootstrap(AppComponent, [
      AccountService,ConnectedAccountService,
      provideStore(connectedAccountReducer) //{} removed.
    ]);

如果您在商店内使用{},请使用效果很好的@Max解决方案: state['reducer function name'].state property