2016-07-30 4 views
0

Store.selectは、私が観察したい店舗のプロパティを示す文字列を取得する必要がありますが、問題はそれらのプロパティがないことです。 彼は、stateプロパティを公開するプロパティとしてreducer関数を持っています。Store.selectバグ?

https://github.com/ngrx/store何かが間違っていることに気付くのは簡単です。

Thierコード:

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

なぜtypescriptはそれらのエラーを投げる?どうすれば修正できますか?

+1

てみてくださいこの状態['connectedAccountReducer']。connectedAccountId' – Max

+0

これは大変感謝しています。なぜ私は['connectedAccountReducer']を追加する必要があるのか​​、型安全性を得るために文字列を使用できないかを知りたいと思います。 –

答えて

0

私のブートストラップ:

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

:包みなさいあなたが提供する店内{}を使用している

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

、素晴らしい作品@Maxソリューションを使用します。 state['reducer function name'].state property