ngrx/store effects 使用总结1:计数器
本教程案例github:https://github.com/axel10/ngrx_demo-counter-and-list
angular2+ 的学习成本应该是三大框架中最高的一个,教程及案例稀缺,流程较为复杂,这里我用计数器和在线获取用户数据并渲染成列表这两个案例来帮大家快速入手angular2+。
在开始之前,希望你能先掌握rxjs以及typescript,否则对其中的一些写法可能会觉得难以理解。
rxjs英文向导:http://reactivex.io/rxjs/manual/overview.html
rxjs中文向导:http://cn.rx.js.org/manual/overview.html
typescipt w3cschool教程:https://www.w3cschool.cn/typescript/
在开始之前,需要先安装@ngrx/store和@ngrx/effects
yarn add @ngrx/store @ngrx/effects
本教程使用的 ngrx/effects和ngrx/store版本均为5.2.0。
先来大致说一下开发流程:
开始 -> 编写数据模型 -> 编写action -> 编写redurces并配置到相应module -> 编写services -> 编写effects并配置到相应module -> 创建组件 -> 组件绑定数据模型 -> 渲染
我们先完成计数器案例。此案例由于没有异步任务,所以可以省略掉services和effects。
从创建项目到启动初始页面之间的步骤这里就不讲了。注意style要使用scss。还有不要使用cnpm安装包。改用yarn或者npm,这样后期使用不容易报错。
- ng new your-project --style scss
第一步:编写数据模型(app/models/num.ts)
- export class Num {
- count: number;
- constructor(num: number) {
- this.count = num;
- }
- }
第二步:编写action(app/actions/num.ts)
- import {Action} from '@ngrx/store';
- export enum NumActionType {
- Add = 'ADD'
- }
- export class ADD implements Action {
- readonly type = NumActionType.Add; //固定写法,必须叫type
- }
第三步:编写redurcers(app/redurces/modelNum.ts)
- import {Num} from '../models/num';
- import {Action} from '@ngrx/store';
- import {NumActionType} from '../actions/num';
- export const modelNum = (state: Num = new Num(0), action: Action) => {
- switch (action.type) {
- case NumActionType.Add:
- state.count++;
- return state;
- default:
- return state;
- }
- };
不要忘记配置redurcer(app/app.module.ts)
- imports: [
- BrowserModule,
- RouterModule.forRoot(routes),
- StoreModule.forRoot({ modelNum}), //配置redurcer
- ],
第四部:创建组件
ng g component model-num
第五步:组件绑定数据模型(连带完成第六步)
组件html文件:
- <div>
- <input (click)="add()" value="+" type="button">
- <p>{{num.count}}</p>
- <input value="-" type="button">
- <br/>
- <a routerLink="/list">to list demo</a>
- </div>
组件ts文件:
- import {Component, OnInit} from '@angular/core';
- import {Num} from '../models/num';
- import {Store} from '@ngrx/store';
- import {NumActionType} from '../actions/num';
@Component({
selector: 'app-model-demo',
templateUrl: './model-demo.component.html',
styleUrls: ['./model-demo.component.scss']
})
export class ModelDemoComponent implements OnInit {
constructor(private _store: Store<any>) {
this._store.select('modelNum').subscribe(mNum => { //涉及到rxjs。
this.num = mNum;
console.log(mNum);
});
}
public num: Num;
public add() {
console.log('add');
this._store.dispatch({ //调用dispatch触发添加redurces
type: NumActionType.Add
});
}
ngOnInit() {
}
}
完成相应的路由配置后,计数器案例完成。
现在开始案例2:在线获取用户列表并展示。
http://www.cnblogs.com/axel10/p/8589139.html
ngrx/store effects 使用总结1:计数器的更多相关文章
- ngrx/store effects 使用总结2:列表展示
第一个计数器案例:http://www.cnblogs.com/axel10/p/8589122.html 完成了计数器案例后,现在开始比较能够完整的展示angular2+开发流程的案例:在线获取用户 ...
- [Angular2] @Ngrx/store and @Ngrx/effects learning note
Just sharing the learning experience related to @ngrx/store and @ngrx/effects. In my personal opinio ...
- [Angular 2] ngrx/store
@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...
- [Angular 2] Using ngrx/store and Reducers for Angular 2 Application State
ngrx/store is a library that simplifies common RxJS patterns for managing state and gives you an eas ...
- NgRx/Store 4 + Angular 5使用教程
这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...
- Angular应用架构设计-3:Ngrx Store
这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...
- [Angulalr] Speed Up Reducer Development Using Ngrx Schematics
When we use NGRX, we need to create some bolipates. Now with Angulalr6, we can use CLI to generate t ...
- [Angular] Improve Server Communication in Ngrx Effects with NX Data Persistence in Angular
Communicating with a remote server via HTTP presents an extra level of complexity as there is an inc ...
- [Angular] How to get Store state in ngrx Effect
For example, what you want to do is navgiate from current item to next or previous item. In your com ...
随机推荐
- Centos下的GitLab的安装汉化和数据备份以及管理员密码重置
前言: 安装版本:gitlab-ce-8.8.5-ce.1.el7.x86_64.rpm 下载地址: https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yu ...
- angular aot编译报错 ERROR in ./src/main.ts 解决方法
昨天打包项目时遇到下图这样的错误: 开始以为了某些模块存在但未使用,折腾一番无果,后来升级angular-cli就搞定了,方法很简单: 1.删掉node_modules 2.更改package.jso ...
- 转载 USB固件分析
http://1438431234.spaces.eepw.com.cn/articles/article/item/114022 0x00000000 0x0001fff0 大小 0x1fff1 = ...
- qt 使用msvc编译器出现乱码如何解决?字符串中存在空格?
开发环境: 1.win7 64位 2.qt版本 windows-x86-msvc2015-5.9.0 如何解决? 1.设置qt文件编码 设置 默认UTF-8 如果编码是 UTF-8 则添加. 2.使用 ...
- 洛谷P3796 - 【模板】AC自动机(加强版)
原题链接 Description 模板题啦~ Code //[模板]AC自动机(加强版) #include <cstdio> #include <cstring> int co ...
- 常用的CSS框架
常用的CSS框架 之前在写自己的个人网站的时候,由于自己Web前端不是特别好,于是就去找相关的CSS框架来搭建页面了. 找到以下这么一篇文章(列出了很多常用的CSS框架): http://w3scho ...
- yum仓库详细解读
Yum:Yellowdog Updater,Modified的简称,起初由yellow dog发行版的开发者Terra Soft研发,用Python编写,后经杜克大学的Linux@Duke开发团队进行 ...
- Davinci DM6446开发攻略——DSP开发工程建立
前段时间一直忙一个项目,同时在生活上时时提防和抵抗中国地沟油.国外核心转基因调和油.大豆油.色拉油.大米玉米.可怕的喂药鱼.药水泡农药喷无虫咬的青菜,所以没时间打理自己的博客,让开发攻略停顿了一段时间 ...
- 【linux】启动apache遇到错误:httpd: Could not reliably determine the server's fully qualified domain name
1)进入apache的安装目录:(视个人安装情况而不同) [root@server ~]# cd /usr/local/apache/conf 2)编辑httpd.conf文件,搜索"#Se ...
- Srtuts2实现登录界面(不连接数据库)报错(四)
1.利用Struts2写一个登录界面,出现以下问题 三月 01, 2014 12:26:18 下午 org.apache.struts2.dispatcher.Dispatcher warn 警告: ...