gpt4 book ai didi

angular - NGRX 效果调度了一个无效的 Action

转载 作者:行者123 更新时间:2023-12-03 13:37:53 26 4
gpt4 key购买 nike

我正在尝试创建一个 @Effect()为了我的行动。当我运行类型为 AuthenticationUserLoad 的操作时我得到一个错误。

ERROR Error: Effect "AuthenticationEffects.getUser$" dispatched an invalid action: [object Object]

这是我的 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})))

);
})
);

更新

我改了 mapswitchMap它有效。
 @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})))
);
})
);

也许我不明白 map 和 switchMap 之间的区别。

最佳答案

map运算符映射当前值,它不关心它是否是可观察的。
鉴于 switchMapmergeMapconcatMap期望回调返回一个可观察的值,订阅它并发出它的值。

因此,当您调用 map您说当前值应该转换为其他值。

map(action => return this.authService.getUser()),
// here the value is an observable stream, but not its emits.

当您调用 switchMap你现在说我想订阅另一个流并发出它的值。因为它是 switchMap它还说一旦父流发出(相同的 Action 出现)取消订阅当前子流并再次订阅 this.authService.getUser() 的新调用返回。

switchMap(action => return this.authService.getUser()),
// here the value is an emit from this.authService.getUser() stream.

关于angular - NGRX 效果调度了一个无效的 Action ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52665641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com