本教程案例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:计数器的更多相关文章

  1. ngrx/store effects 使用总结2:列表展示

    第一个计数器案例:http://www.cnblogs.com/axel10/p/8589122.html 完成了计数器案例后,现在开始比较能够完整的展示angular2+开发流程的案例:在线获取用户 ...

  2. [Angular2] @Ngrx/store and @Ngrx/effects learning note

    Just sharing the learning experience related to @ngrx/store and @ngrx/effects. In my personal opinio ...

  3. [Angular 2] ngrx/store

    @ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJ ...

  4. [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 ...

  5. NgRx/Store 4 + Angular 5使用教程

    这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer ...

  6. Angular应用架构设计-3:Ngrx Store

    这是有关Angular应用架构设计系列文章中的一篇,在这个系列当中,我会结合这近两年中对Angular.Ionic.甚至Vuejs等框架的使用经验,总结在应用设计和开发过程中遇到的问题.和总结的经验, ...

  7. [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 ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 让网站通过Https访问

    Prerequisites Before you begin, you should have some configuration already taken care of. We will be ...

  2. Egret学习笔记 (Egret打飞机-1.大致思路)

    大致看了一遍Egret的官方文档,就开始打算使用Egret来开发一个打飞机游戏. 首先来捋一捋思路,先来看一看一个打飞机游戏的图片 基本上一个打飞机游戏分为 开始游戏   ----------进入游戏 ...

  3. CodeForces - 796C Bank Hacking

    思路:共有n-1条边连接n个点,即形成一棵树.一开始需要选择一个点hack--将这个点视为根结点,与它相邻的点防御值加1,与它相隔一个在线点的点的防御也加1.当根节点被hack,即这个点被删除,又变成 ...

  4. clearstatcache

    clearstatcache clearstatcache — 清除文件状态缓存 void clearstatcache ([ bool $clear_realpath_cache = false [ ...

  5. jVM笔记4-对象的结构

    1.对象的结构有: 1.Header(对象头),其组成主要有两部分: 1.自身运行时的数据(Mark Word),包括: 1.哈希值 2.GC分代年龄. 3.锁状态标志 4.线程所持有的锁 5.偏向线 ...

  6. Hbase配置java客户端

    1.修改windows配置文件 C:\WINDOWS\system32\drivers\etc\hosts 将远程hbase和zookeeper主机的IP地址加进去 54.0.88.53      H ...

  7. Windows10系统故障检测你知道多少-上海IT33

    Windows 10作为微软公司最新的一款操作系统,从使用的方便和界面的整洁上来说,固然是很好的,但是其因为隐私问题,致使很多人不惜一切代价想要远离Windows 10这款操作系统.尽管Windows ...

  8. 【其他】3dmax撤销Ctrl+z不能用的解决办法

    转载请注明出处:http://www.cnblogs.com/shamoyuu/p/3dmax_ctrlz.html 如果你经常去网上下载各种模型参考学习的话,出现这个问题的概率会非常高.因为出现这个 ...

  9. R︱mlr包帮你挑选最适合数据的机器学习模型(分类、回归)+机器学习python和R互查手册

    一.R语言的mlr packages install.packages("mlr")之后就可以看到R里面有哪些机器学习算法.在哪个包里面. a<-listLearners() ...

  10. CSDN博客添加量子恒道统计代码步骤

    CSDN博客添加量子恒道统计代码步骤. 1. 去量子恒道网站统计 注册账户: 2. 添加已有的CSDN博客地址: 3. 添加博客后恒道代码里面会给你一个JavaScript脚本,记下里面的一串数字: ...