本教程案例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. 依赖于boodtrap3的插件推荐以及bootrap发展前景

    作为一个栅格系统和速度开发的,偏向于框架,bootstrap出来很火,为了节省效率,不少公司选用这个框架进行开发,一同被发现的是依赖于bootrap各种插件的adminLTE的集成模版,但是前端框架日 ...

  2. c++实现一个小算法

    题目:有n个格子,每个格子里有坦克,坦克有两滴血,你向格子里投掷炸弹,每次命中坦克他掉一滴血并随机像左或者右移动一个格子,问最少炸几次能把全部坦克炸完. 题解:先向偶数格子投掷炸弹,所有的坦克全跑到奇 ...

  3. C语言_了解一下C语言中的四种存储类别

    C语言是一门通用计算机编程语言,应用广泛.C语言的设计目标是提供一种能以简易的方式编译.处理低级存储器.产生少量的机器码以及不需要任何运行环境支持便能运行的编程语言. C语言中的四种存储类别:auto ...

  4. MysqL主从复制_模式之GTID复制

    基于GTID的复制是从Mysql5.6开始支持的一种新的复制方式,此方式与传统基于日志的方式存在很大的差异,在原来的基于日志的复制中,从服务器连接到主服务器并告诉主服务器要从哪个二进制日志的偏移量开始 ...

  5. 算法提高 金属采集 树形DP

    题目链接:金属采集 思路:d(i, j)表示在以i为根结点的子树中使用j个机器人的最小花费.设v为u的一个子节点,从节点i使用k个机器人收集以v为根结点的能量,状态转移方程为d(u, i) = min ...

  6. 用pycharm+flask 建立项目以后运行出现ImportError: No module named flask-login问题

    出现此问题,一般情况下: 打开CMD输入: pip install flask-login 然后,在cmd中输入命令: pip list 查看目前已安装的的模板.在此时,如果你继续运行项目,有可能会发 ...

  7. win10安装mongodb及配置 和 mongodb的基本使用(node环境)

    mongodb安装 下载地址: https://www.mongodb.com/download-center 下载后,我们点击mongodb-win32-x86_64-2008plus-ssl-3. ...

  8. Vue中method与computed的区别

    为了说明method与computed的区别,在此我想先来看看computed属性在vue官网中的说法:模板内的表达式是非常便利的,但是它们实际上只用于简单的运算.在模板中放入太多的逻辑会让模板过重且 ...

  9. swift 之归档和解归档

    swift 之归档和解归档 数据持久化的方式有很多种,归档是其中的一种,说起数据持久化的方式,iOS 中基本有以下几种方式:sqlite存储.coredata存储.UserDefault存储.归档.p ...

  10. Linux传统Huge Pages与Transparent Huge Pages再次学习总结

      Linux下的大页分为两种类型:标准大页(Huge Pages)和透明大页(Transparent Huge Pages).Huge Pages有时候也翻译成大页/标准大页/传统大页,它们都是Hu ...