@ngrx/store builds on the concepts made popular by Redux and supercharges it with the backing of RxJS. The result is a tool and philosophy that will transform the way you approach state management in your Angular 2 applications. This lesson takes an existing Angular 2 app and refactors it to utilize @ngrx/store, touching on all of the major concepts along the way!

Link: https://github.com/btroncone/ngrx-store-in-ten

The approach I took is a little bit different from the one shown in Github.

People reducer:

For perople reducer, mainly three things:

  • Add person
  • Remove person
  • Toggle person state
export const people = (state = defaultPeople, {type, payload}) => {
switch(type){
case TOGGLE_STATE:
return state.map( (person) => {
if(person.name === payload.name){
let state = person.state ? !person.state : true;
return Object.assign({}, person, {state});
}
return person;
});
case ADD_PERSON:
return [
...state,
{name: payload, time: new Date()}
];
case DELETE_PERSON:
var index = state.indexOf(payload);
console.log(index);
return [
...state.slice(, index),
...state.slice(index+),
];
default:
return state;
}
};

Then on the interface, add input and buttons:

        <ul>
<li [style.textDecoration]="person.state ? 'line-through': 'none'" (click)="person$.next(person)" *ngFor="#person of people | async">
{{person.name}} is in {{person.time | date : 'jms'}}
<button (click)="deletePerson$.next(person)">Remove</button>
<button (click)="toggleState(person)">Toggle</button>
</li>
</ul>
<br>
<input type="text" #personInp><button (click)="addPerson$.next(personInp.value); personInp.value=''">Add</button>

Add Person:

    addPerson$ = new Subject()
.map( (person) => ({type: ADD_PERSON, payload: person}));

Here we create an Action: {type: ADD_PERSON, payload: person}.

Dispatch the action:

        Observable.merge(
...
this.addPerson$,
...
)
.subscribe(store.dispatch.bind(store))

Toggle Person:

For add person, we use Subject() to emit the event. For toggle person, we just use normal function to dispatch the action:

    toggleState(person){
this.store.dispatch({type: TOGGLE_STATE, payload: person})
}

Filter:

Filter reducer add function which will be passed into the Array.filter() function:

export const filter = (state = person => person, {type, payload}: {type: ""}) => {
switch(type){
case SHOW_ALL:
return person => person;
case SHOW_AVAILABLE:
return person => !person.state;
case SHOW_BUSY:
return person => person.state;
default:
return state;
}
}

Tamplete:

        <button (click)="all$.next()">Show All</button>
<button (click)="available$.next()">Show Available</button>
<button (click)="busy$.next()">Show Busy</button>

Use Subject:

    all$ = new Subject()
.mapTo({type: SHOW_ALL}); available$ = new Subject()
.mapTo({type: SHOW_AVAILABLE}); busy$ = new Subject()
.mapTo({type: SHOW_BUSY});

Dispatch:

        Observable.merge(
this.person$,
this.addPerson$,
this.deletePerson$,
this.available$,
this.all$,
this.busy$
)
.subscribe(store.dispatch.bind(store))

Update store:

        this.people = Observable.combineLatest(
this.people,
this.filter,
( people, filter) => {
return people.filter(filter);
}
);

-------------------------

[Angular 2] ngrx/store的更多相关文章

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

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

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

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

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

  4. ngrx/store effects 使用总结1:计数器

    本教程案例github:https://github.com/axel10/ngrx_demo-counter-and-list angular2+ 的学习成本应该是三大框架中最高的一个,教程及案例稀 ...

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

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

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

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

  7. [转]VS Code 扩展 Angular 6 Snippets - TypeScript, Html, Angular Material, ngRx, RxJS & Flex Layout

    本文转自:https://marketplace.visualstudio.com/items?itemName=Mikael.Angular-BeastCode VSCode Angular Typ ...

  8. [Angular 2] @ngrx/devtools demo

    Check the Github: https://github.com/ngrx/devtools Example:

  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. 判断iPhone和iPad 判断设备版本

    //判断iPhone和iPad #define IS_IPHONE (!IS_IPAD) #define IS_IPAD (UI_USER_INTERFACE_IDIOM() != UIUserInt ...

  2. 安卓自写Adapter

    代码: package com.example.ouradapter; import android.app.ListActivity; import android.content.Context; ...

  3. cxf客户端代码wsdlLocation设置相对路径

    利用工生成的cxf客户端代码,wsdlLocation都是绝对路径,为了便于项目更加灵活管理,我们可以将该路径设置为相对路径: 1.下面图片是我的项目路径图片及wsdl地址存放路径: 2.下面图片是我 ...

  4. SAS学习笔记

    一.            在SAS中进行随机抽样: 1. 在实际数据处理中常常需要进行样本抽样,在实践中主要有两种情况: (1)简单无重复抽样(2)分层抽样   a.等比例分层抽样  b. 不等比例 ...

  5. [Struts2学习笔记] -- 自定义类型转换

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...

  6. 搬瓦工搭建VPN

    搬瓦工VPS的PPTP VPN搭建其实和上面都是很简单的,这个要用到上面的搬瓦工VPS控制面板. Root shell - advanced 下载 centos 6 一键安装包 wget --no-c ...

  7. 使用putty登陆cygwin出现server unexpectedly ...error.解决方案

    将cygwin安装目录下/etc/passwd中的passwd文件中user:unused:32707:10513:U-CYOU-INC\user,S-1-5-21-2645613570-259884 ...

  8. grunt serve Fatal error: Port 35729 is already in use by another process.

    y@y:~$ lsof | grunt y 0t0 TCP *: (LISTEN) Optimizin y 0t0 TCP *: (LISTEN) v8:Sweepe y 0t0 TCP *: (LI ...

  9. 【Java】基本数据类型长度

    byte----1 char----2 short----2 int-----4 long------8 float---4 double----8

  10. DEDECMS调用最新评论

    {dede:feedback row='5' titlelen='24' infolen='80'} <div class="yhplk"><div>[fi ...