[Angular] Improve Server Communication in Ngrx Effects with
Communicating with a remote server via HTTP presents an extra level of complexity as there is an increased chance of race conditions and the need for error handling. There is also the tricky problem of how to handle the user experience as the application is trying to complete the remote request. NX Data Persistence is a library that works with ngrx effects to give us a better way to handle remote server calls as well as improving on the overall shape of the effect itself. In this lesson,we are going to convert our stock effects to use NX Data Persistence and the advantages of doing so by using? ? Install: npm i --save @nrwl/nx ? app.module.ts:
import { NxModule } from ‘@nrwl/nx‘;
? effect: import { Injectable } from ‘@angular/core‘; import {Effect} from ‘@ngrx/effects‘; import { DataPersistence } from ‘@nrwl/nx‘; import {Action} from ‘@ngrx/store‘; import { RolesService } from ‘../services/roles.service‘; import { Observable } from ‘rxjs‘; import { switchMap,map } from ‘rxjs/operators‘; import * as actions from ‘../actions‘; import { Role } from ‘../models‘; import { DashbaordState } from ‘../reducers‘; @Injectable() export class RolesEffects { constructor( private rolesService: RolesService,private dataPersistence: DataPersistence<DashbaordState> ) { } @Effect() loadRoles$: Observable<Action> = this.dataPersistence.fetch( actions.RolesActionTypes.LoadRoles,{ run: (action: actions.LoadRoles) => { return this.rolesService.roles.pipe( map((roles: Role[]) => new actions.LoadRolesSuccess(roles)) ); },onError: (action: actions.LoadRoles,error) => { console.error(‘Error‘,action,error); } } ); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- vim – 如何设置“:make”来使用scons?
- 角度2.0材料MdDialog与角度2.0的工作实例
- bootstrap折叠调用collapse()后data-parent不生效的快速解决
- WebService传递大字符串
- docker镜像列表存在但删除显示 No such image问题解决
- 关于webservice的异步调用简单实例
- angularjs – 将数据从一个指令传递到另一个指令
- No.21 Yum(Yellowdog Updater Modified)软件管理
- twitter-bootstrap – Angular bootstrap下拉切换 – 通过键
- bootstrap-wysiwyg: 迷你的Bootstrap的所见即所得的HTML富文