Tom*_*omP 7 typescript ngrx ngrx-effects angular
我正在尝试@Effect()为我的操作创建一个。当我使用 type 运行 action 时,AuthenticationUserLoad出现错误。
ERROR Error: Effect "AuthenticationEffects.getUser$" dispatched an invalid action: [object Object]
Run Code Online (Sandbox Code Playgroud)
这是我的Effect代码
@Effect()
getUser$ = this.actions$.pipe(
ofType(AuthenticationUserActions.AuthenticationUserTypes.AuthenticationUserLoad),
map((action) => {
return this.authService.getUser().pipe(
map((user: User) => new AuthenticationUserActions.AuthenticationUserLoadSuccess({user})),
catchError(error => of(new AuthenticationUserActions.AuthenticationUserLoadFailure({error})))
);
})
);
Run Code Online (Sandbox Code Playgroud)
更新
我改变map了switchMap,它的工作原理。
@Effect()
getUser$ = this.actions$.pipe(
ofType(AuthenticationUserActions.AuthenticationUserTypes.AuthenticationUserLoad),
switchMap((action) => {
return this.authService.getUser().pipe(
map((user: User) => new AuthenticationUserActions.AuthenticationUserLoadSuccess({user})),
catchError(error => of(new AuthenticationUserActions.AuthenticationUserLoadFailure({error})))
);
})
);
Run Code Online (Sandbox Code Playgroud)
也许我不明白 map 和 switchMap 之间的区别。
map运算符映射当前值,它不关心它是否是可观察的。而switchMap或mergeMap或concatMap期望回调返回可观察值,则订阅它并发出它的值。
因此,当您致电时,map您会说当前值应该转换为其他值。
map(action => return this.authService.getUser()),
// here the value is an observable stream, but not its emits.
Run Code Online (Sandbox Code Playgroud)
当你打电话时switchMap你说现在我想订阅另一个流并发出它的值。因为它还switchMap表示,一旦父流发出(出现相同的操作),就取消订阅当前的子流并再次订阅新的调用this.authService.getUser()返回的内容。
switchMap(action => return this.authService.getUser()),
// here the value is an emit from this.authService.getUser() stream.
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9704 次 |
| 最近记录: |