2017-03-02 74 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运营商将扁平化包含两个动作,使这两个动作是阵列发射 - 按照它们在阵列中声明的顺序排列。