2017-03-02 17 views
1

我的應用程序使用ngrx和ngrx效果。這裏是我的應用程序的影響之一:鏈接RxJS映射運算符和ngrx效果問題

@Effect() 
    reloadPersonalInfo$: Observable<Action> = this.actions$ 
    .ofType(currentUserAccount.ActionTypes.RELOAD_PERSONAL_INFO) 
    .filter(() => <boolean>JSON.parse(localStorage.getItem('authenticated'))) 
    .switchMap(() => 
     this.userAccountService 
     .retrieveCurrentUserAccount() 
     .map(currentUserAccount => new LoadUserAccountAction(currentUserAccount)) 
     .map(() => new SigninAction()) 
    ); 

我很奇怪,爲什麼LoadUserAccountAction沒有進入我的減速功能,如果我註釋掉//.map(() => new SigninAction())

任何人都可以請幫助除?我錯了什麼?

+0

效果意味着只返回一個動作。 – Jim

+0

@Jim:感謝您的評論和編輯。你能否建議一種方法來改變應用程序的設計,以避免返回幾個效果的行動? – balteo

+0

您正試圖同時返回「LoadUserAccountAction」和「SigninAction」。你應該返回其中一個。換句話說,刪除代碼中的一行.map行。 – Jim

回答

2

LoadUserAccountAction沒有出動,因爲它不是由效果發出,作爲最終.map(() => new SigninAction())是看到了SigninAction發出來代替。

有可能從效果發出多個動作,你只需要像這樣做:

@Effect() 
reloadPersonalInfo$: Observable<Action> = this.actions$ 
    .ofType(currentUserAccount.ActionTypes.RELOAD_PERSONAL_INFO) 
    .filter(() => <boolean>JSON.parse(localStorage.getItem('authenticated'))) 
    .switchMap(() => this.userAccountService 
    .retrieveCurrentUserAccount() 
    .concatMap(currentUserAccount => [ 
     new LoadUserAccountAction(currentUserAccount), 
     new SigninAction() 
    ]) 
); 

concatMap運營商將扁平化包含兩個動作,使這兩個動作是陣列發射 - 按照它們在陣列中聲明的順序排列。