正如标题所说,我正在使用 rxjs,在其中我调用一个服务并返回
UnsubscriptionErrorImpl {message: "1 errors occurred during unsubscription:\n1) TypeEr…not read properties of undefined (reading 'type')", name: 'UnsubscriptionError', errors: Array(1)}
我的代码生效了.ts
loadUserProfile$ = createEffect(() =>
this.actions.pipe(
ofType(ActionTypes.LoadUserProfile),
take(1),
switchMap (_ => this.userProfileService.getUserProfile().pipe(take(1), tap(res => console.log(res))) )));
我在 userProfileService 中的代码
public getUserProfile(): Observable<any> {
return this.httpClient.get(this.accountBaseUrl + '/admin/users/me');
}
我的代码似乎是正确的,但我仍然收到取消订阅错误。
修改您的代码如下:
import { catchError, map } from 'rxjs/operators';
// ...
loadUserProfile$ = createEffect(() =>
this.actions.pipe(
ofType(ActionTypes.LoadUserProfile),
switchMap(_ =>
this.userProfileService.getUserProfile().pipe(
map(res => ({ type: ActionTypes.LoadUserProfileSuccess, payload: res })),
catchError(error => of({ type: ActionTypes.LoadUserProfileFailure, payload: error }))
)
)
)
);
您可以使用 catchError 运算符来捕获并处理内部可观察对象内部发生的任何错误