@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. sql - sum() 和 count() 函数的区别

    对sql一直都是蜻蜓点水,突然也觉得对这两个函数的区别有点朦胧,查了一下,在这里说一下: sum():主要用于累加求和. count():主要用于行(记录)的统计.

  2. 99%Bug 修复方法

    以下仅支持4.0.3或之后的设备 (写在前面,这个教程需要安装两个app,且卸载后无法达到效果,所以有app drawer洁癖者慎重(你可以把它们隐藏起来么). 当然等官方rom更新或者安装最新三方r ...

  3. Ipad亚麻布纹背景-最终效果_学习教程

  4. Extjs嵌入html

    方式一:使用组件的html属性嵌入html代码,如果html代码中存在参数可以使用字符串拼接的方式拼接html代码. html页面: <!doctype html> <html> ...

  5. js中的 window.location、document.location、document.URL 对像的区别(转载)

    原文:http://www.cr173.com/html/18417_1.html 当我们需要对html网页进行转向的时候或是读取当前网页的时候可以用到下面三个对像: window.location. ...

  6. MYSQL 存储过程1、SQL存储过程的基础知识

    在深入理解MySq之前,我们先理下一些简单的问题 Q:什么是存储过程?(stored procedure) A:是一段写好的SQL代码,特别的就是它是存在数据库的目录里.所以外部程序可以直接调用数据库 ...

  7. C# Ini文件操作

    在开源中国看到的操作ini文件的,写的还不看,留着以后用 using System; using System.IO; using System.Runtime.InteropServices; us ...

  8. [解决]UserLibrary中的jar包不会自动发布Tomcat的lib目录下(基于MyEclipse2014)

    1.在工程名称上单击[右键] —— 单击[Properties]选项,点击后会弹出属性窗口: 2.选择[Properties]后在左侧树中找到[MyEclipse] —— [Deployment As ...

  9. testservice小项目总结

    关于自做小项目testservice的总结: 1.Activity与Service的绑定及之间的通信: 1)关于Activity和Service的生命周期的理解: 2)bindService方法中Se ...

  10. spinner 下拉框控件

    spinnerMode=dropdown时,为下拉模式spinnerMode=dialog时,会在界面中间弹出Android:popupBackground=”#f0000000”,可以去除spinn ...