0

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'.
      });

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

4

1 回答 1

0

我的助推器:

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

变成:

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

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

于 2016-07-30T22:54:02.883 回答