如果这是一些问题的重复,我最近很抱歉,但我找不到任何解决我的问题的方法。
我有警卫,现在我需要从调用 http 服务并将用户重定向到错误状态的副作用中捕获错误。尝试添加catchError
,但如果我从服务器端收到错误,我什至不会去那里。
守卫.ts
canActivate(): Observable<boolean> {
return this.store
.select(fromDataStore.getDatasLoaded)
.pipe(
withLatestFrom(this.store.select(fromDataStore.getDataLoading)),
map(([loaded, loading]) => {
if (!loaded && !loading) {
this.store.dispatch(new fromDataStore.LoadData());
}
return loaded;
}),
filter(loaded => loaded),
take(1),
catchError(error => {
// redirect user to error state
})
);
}
效果.ts
@Effect() loadData$: Observable<Action> = this.actions$.ofType(DataActionTypes.LoadData)
.pipe(
mergeMap(() =>
this.dataApiService.loadData()
.pipe(
map(data => ({
type: DataActionTypes.LoadDataSuccess,
payload: data
})),
catchError(() => of({ type: DataActionTypes.LoadDataFailure }))
)
)
);
减速器.ts
case DataActionTypes.LoadData: {
return {
...state,
data: {
...state.data,
isLoading: true
}
};
}
case DataActionTypes.LoadDataSuccess: {
return {
...state,
data: {
...dataAdapter.addMany(action.payload, state.data),
isLoaded: true,
isLoading: false
}
};
}
case DataActionTypes.LoadDataFailure: {
return {
...state,
data: {
...state.data,
isLoaded: true,
isLoading: false
}
};
}
default: {
return state;
}