2016-09-25 44 views
0

繞圈試圖弄清楚這一點......ng2-redux @select返回AnonymousSubject

新的還原。剛剛進入Angular2 ...

我試圖從accountReducer選擇一個用戶帳戶對象

const INITIAL_STATE: IAccount = { 
    anonymous: true, 
    email:"" 
}; 


export function accountReducer(state: IAccount = INITIAL_STATE, action:any):IAccount 
{ 
    switch (action.type) { 
     default: 
     return state; 
    } 
} 

這是rootReducer:

export interface IAppState { 
    account?: IAccount; 
} 

/** 
* root reducer 
*/ 
export default combineReducers<IAppState> ({ 
    account: accountReducer 
}); 

然後,當我選擇註冊頁面上該帳戶我得到一個AnonymousSubject對象。

export class AccountSignupFormComponent { 

    //@select(state => state.account) account; 
    @select() account; 

    constructor(
     private _redux: NgRedux<IAppState>, 
     private _actions: AccountActions 

    ){} 

ngOnInit(){ 
    console.log(this.account); 
    // account is an AnonymousSubject from RxJs. 
} 

如果我看一下this._redux.getState(),那麼狀態會在那裏看起來正確。

我猜我錯過了一些簡單的東西,但我沒有看到它。

任何人都能看到我失蹤的東西嗎?

回答

0

這是正確的。 看看源代碼

function getter() { 
     return NgRedux.instance.select(bindingKey, comparator); 
} 

https://github.com/angular-redux/ng2-redux/blob/v4.0.0-beta.3/src/decorators/select.ts#L35

,並在這一行:

select<S>(
     selector: PropertySelector | 
      PathSelector | 
      FunctionSelector<RootState, S>, 
      comparator?: Comparator): Observable<S> { 
... 
let result: Observable<S>; 
... 
return result.distinctUntilChanged(comparator); 

https://github.com/angular-redux/ng2-redux/blob/v4.0.0-beta.3/src/components/ng-redux.ts#L135

你可以在你的模板與async管使用它,或者只是訂閱它。

這裏是Plunker Example