链接 RxJS 地图运算符和 ngrx 效果问题
Chaining RxJS map operators and ngrx effects Issue
我的应用使用了 ngrx 和 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
不进入我的 reducer 函数,除非我注释掉 //.map(() => new SigninAction())
有人可以帮忙吗?我错了什么?
你的 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
运算符将展平包含两个动作的数组,以便发出两个动作 - 按照它们在数组中声明的顺序。
我的应用使用了 ngrx 和 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
不进入我的 reducer 函数,除非我注释掉 //.map(() => new SigninAction())
有人可以帮忙吗?我错了什么?
你的 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
运算符将展平包含两个动作的数组,以便发出两个动作 - 按照它们在数组中声明的顺序。