Angular2 ngrx / store用于处理失败的HTTP请求
发布时间:2020-12-17 09:29:14 所属栏目:安全 来源:网络整理
导读:我想有一个简单的代码路径来创建和分派HTTP操作.我想做的是: this.http.request(...) .map((res: Response) = res.json()) .catch((err: any) = err.json()) .map((payload: any) = { type: 'SUCCESS',payload }) .catch((payload: any) = { type: 'FAILURE
我想有一个简单的代码路径来创建和分派HTTP操作.我想做的是:
this.http.request(...) .map((res: Response) => res.json()) .catch((err: any) => err.json()) .map((payload: any) => { type: 'SUCCESS',payload }) .catch((payload: any) => { type: 'FAILURE',payload}) .subscribe((action: Action) => this.store.dispatch(action)); 这样,成功和失败响应都转换为JSON,然后根据成功/失败标准分配正确的减少类型,以便可以正确地操作商店. (认为??用户登录成功和失败,返回200或401). 是否有更清洁或更好的处理方式?当前第二个.catch不能很好地发挥作用,因为它没有返回一个可观察的. 建议或其他解决方案欢迎?
在我的一项服务中,我这样做:
get(url,actionType) { this._http.get(BASE_URL + url) .map(response => response.json()) .map(payload => ({ type: actionType,payload })) .subscribe(action => this.store.dispatch(action),error => this._apiErrorHandler(error)); } private _apiErrorHandler(response) { let payload = response.json().error; this.store.dispatch({ type: 'API_ERROR',payload }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 【数据结构】第2周 栈与队列 4:中缀表达式的值
- 《jQuery从入门到精通》第三十九节 创建我的bootstrap页面
- Bootstrap简介
- scala – 将一个列表转换为猫ValidatedNel
- Angular 2,Webpack Uncaught ReferenceError:vendor …在添
- twitter-bootstrap – 将Bootstrap列堆叠在一起
- bash – 如何在文件中固定数量的字符后插入新的行字符
- 数组 – 如果Scala中需要不可变数组,返回IndexesSeq而不是A
- 在bash中查找重复文件的时间复杂性
- AngularJS 2错误:未定义enableProdMode