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'.
});
为什么打字稿会抛出这些错误?我该如何解决?