[Angular 2] Using Two Reducers Together
Add another reducer:
export const SECOND = "SECOND";
export const HOUR = "HOUR"; export const clock = (state = new Date(), {type, payload} = {type: ""})=> {
const date = new Date(state.getTime());
switch(type){
case SECOND:
date.setSeconds(date.getSeconds() + payload);
return date; case HOUR:
date.setHours(date.getHours() + payload);
return date;
default:
return state;
} return state;
}; const defaultPeople = [
{name: "Sara", time: clock()},
{name: "Wan", time: clock()},
{name: "Zhen", time: clock()},
{name: "Tian", time: clock()}
];
export const people = (state = defaultPeople, {type, payload}) => {
switch(type){ default:
return state;
}
};
Added a 'people' reducer, defined a 'defaultPeople' as default state.
// main.ts bootstrap(App, [
provideStore({clock, people})
])
In bootstrap, add people reducer to the provideStore().
Then finally inside app.ts, use log out people information:
/**
* Created by wanzhen on 26.4.2016.
*/
import {Component} from 'angular2/core';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/interval';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/merge';
import 'rxjs/add/operator/startWith';
import 'rxjs/add/operator/scan';
import 'rxjs/add/operator/mapTo';
import {Subject} from "rxjs/Subject";
import {Store} from '@ngrx/store';
import {SECOND, HOUR} from './reducers';
import {ClockComponent} from './clock'; @Component({
selector: 'app',
directives: [ClockComponent],
template: `
<input #inputNum type="number" value="">
<button (click)="click$.next(inputNum.value)">Update</button>
<clock [time]="time |async"></clock>
<ul>
<li *ngFor="#person of people | async">
{{person.name}} is in {{person.time | date : 'jms'}}
</li>
</ul>
`
})
export class App {
click$ = new Subject()
.map( (number) => ({type: HOUR, payload: parseInt(number)})); seconds$ = Observable.interval()
.mapTo({type: SECOND, payload: }); time;
people; constructor(store:Store) {
this.time = store.select('clock');
this.people = store.select('people'); Observable.merge(
this.click$,
this.seconds$
)
.subscribe(store.dispatch.bind(store))
}
}
[Angular 2] Using Two Reducers Together的更多相关文章
- [Angular 2] Passing Template Input Values to Reducers
Angular 2 allows you to pass values from inputs simply by referencing them in the template and passi ...
- [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 ...
- [Angular] Expose Angular Component Logic Using State Reducers
A component author has no way of knowing which state changes a consumer will want to override, but s ...
- [Angular 2] Dispatching Action with Payloads and type to Reducers
While action types allow you tell your reducer what action it should take, the payload is the data t ...
- angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”
曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示r ...
- Redux你的Angular 2应用--ngRx使用体验
Angular2和Rx的相关知识可以看我的Angular 2.0 从0到1系列第一节:Angular 2.0 从0到1 (一)第二节:Angular 2.0 从0到1 (二)第三节:Angular 2 ...
- React和Angular
React和Angular 你若装逼,请带我飞! 从前,从前,听说React只负责UI,话说写Angular代码就像写后端,现在看来,React赢在情怀上了: 我认为没必要老是拿React和Angul ...
- 翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2
翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2 原文地址:http://onehungrymind.com/build-better-angular-2-application- ...
- Angular vs React 最全面深入对比
如今,Angular和React这两个JavaScript框架可谓红的发紫,同时针对这两个框架的选择变成了当下最容易被问及或者被架构设计者考虑的问题,本文或许无法告诉你哪个框架更优秀,但尽量从更多的角 ...
随机推荐
- Activity之间的数据传递(Arraylist)
1.使用Serialiable方法 实现序列化 2.使用Parcelable方法(这是android自己封装的类) Parcel类是封装数据的容器,封装后的数据通过Intent和IPC传递 实 ...
- Java 取整
向上取整用Math.ceil(double a) 向下取整用Math.floor(double a) 举例: public static void main(String[] args) throws ...
- iPhone真机测试Crash信息分析
一.获取Crash Log的方式 在iOS开发过程,当应用已经打包,iPhone设备通过ipa的包安装应用后,在使用过程发现crash,那么如何获取crash日志呢,现提供如下四种获取crash日志的 ...
- emmt html生成
html:5 或 ! html:5 或!:用于HTML5文档类型 html:xt:用于XHTML过渡文档类型 html:4s:用于HTML4严格文档类型 常用过渡文档类型 html:xt 直接c ...
- ERROR:the server has either erred or is incapable of performing the requested operation
openstack中,有时会经常出现这种错误,原因无二,一是安全组没有设置正确,二是openstack中网络配置会有些问题或者是相关的服务没有启动. 解决方法:1.安全组问题在nova.conf和ne ...
- Chrome 中的 JavaScript 断点设置和调试技巧 (转载)
原文地址:http://han.guokai.blog.163.com/blog/static/136718271201321402514114/ 你是怎么调试 JavaScript 程序的?最原始的 ...
- sql语句去除重复记录(多表连接的查询)
--处理表重复记录(查询和删除) /********************************************************************************** ...
- linux指令tips
1.调用命令使用应用名称免路径. 例如在路径 /usr/local/mobile/php538 建立了php应用,在调用php命令的时候,我们需要加路径访问 如 /usr/local/mobile ...
- saas系统架构经验总结
2B Saas系统最近几年都很火.很多创业公司都在尝试创建企业级别的应用 cRM, HR,销售, Desk Saas系统.很多Saas创业公司也拿了大额风投.毕竟Saas相对传统软件的优势非常明显. ...
- mysql操作之二
特殊数据类型 表约束 表连接 索引 触发器 安全性 DB设计 alter table student modify id int primary key; 主銉不可重复修改 alter table s ...