[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 that your reducer will use to update the state.
// reducer.ts export const SECOND = "SECOND";
export const HOUR = "HOUR"; export const clock = (state = new Date(), {type, payload})=> {
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; } return state;
};
//app.ts /**
* 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 './reducer'; @Component({
selector: 'app',
template: `
<button (click)="click$.next()">Update</button>
<h1>{{clock | async | date:'yMMMMEEEEdjms'}}</h1>
`
})
export class App {
click$ = new Subject(); clock; constructor(store:Store) {
this.clock = store.select('clock'); Observable.merge(
this.click$.mapTo({type: HOUR, payload: }),
Observable.interval().mapTo({type: SECOND, payload: })
)
.subscribe((action)=>{
store.dispatch(action)
})
}
}
// main.ts import {bootstrap} from 'angular2/platform/browser';
import {App} from './app';
import {provideStore} from '@ngrx/store';
import {clock} from './reducer'; bootstrap(App, [
provideStore({clock})
]).then(
()=> console.log('App running...'),
err=> console.log(err)
); /*
* 1. Create a reducer
* 2. Use provideStore({reducer_name}) to provide store
* 3. Use store.select('reducer_name') to get store in Observable type
* 4. Apply logic to dispatch the action
* */
[Angular 2] Dispatching Action with Payloads and type to Reducers的更多相关文章
- [Angular] Ngrx/effects, Action trigger another action
@Injectable() export class LoadUserThreadsEffectService { constructor(private action$: Actions, priv ...
- angular初步认识一
最近比较流行MVC前端框架开发,最近研究了一个框架AngularJS框架 不说那么多,先上例子,我是个代码控 <!DOCTYPE html> <html lang="en& ...
- [Angular 2] Handle Reactive Async opreations in Service
When you use ngrx/store and you want to fire a service request. When it sucessfully return the respo ...
- [转] How to dispatch a Redux action with a timeout?
How to dispatch a Redux action with a timeout? Q I have an action that updates notification state of ...
- [Angular 2] Using ng-model for two-way binding
Two-way binding still exists in Angular 2 and ng-model makes it simple. The syntax is a combination ...
- 翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2
翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2 原文地址:http://onehungrymind.com/build-better-angular-2-application- ...
- angular笔记_7
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 在angular中利用分页插件进行分页
必需:angular分页js和css 当然还有angular.js 还需要bootstrap的css angular.min.js (下面我直接把插件粘贴上去了,以免有的同学还要去找.是不是很贴 ...
- Redux 和 ngrx 创建更佳的 Angular 2
Redux 和 ngrx 创建更佳的 Angular 2 翻译:使用 Redux 和 ngrx 创建更佳的 Angular 2 原文地址:http://onehungrymind.com/build- ...
随机推荐
- target,currentTarget,delegateTarget,srcElement
第一种情况:就是IE9+和其他现代浏览器,支持addEventListener方法.其结果是: this总是等于currentTarget currentTarget总是事件监听者 target总是事 ...
- 一个tomcat部署俩个java web项目
2.发布的时候可以发布成war包,用项目名称右键export,选择项目名称,还有发布的路径,即tomcat下的路径,参考http://zhidao.baidu.com/link?url=imOu0Uu ...
- 【OpenSSL】创建证书
[-] 1生成根证书 1 生成RSA私钥 2 生成证书请求 3 签发自签名证书 2 生成用户证书 1 生成RSA私钥 2 生成证书请求 3 签发证书 1)生成根证书 1.1) 生成RSA私钥 op ...
- 你好,C++(38)从问题描述中发现对象的属性和行为 6.4 工资程序成长记:类与对象(上)
6.4 工资程序成长记:类与对象 “夜半三更哟,盼天明:寒冬腊月哟,盼春风.若要盼得哟,涨工资,岭上……”自从上次老板许诺给小陈涨工资以后,一转眼又过去几个月了,可是涨工资的事一点动静都没有.小陈只 ...
- nginx反向代理取得IP地址
nginx反向代理后,在应用中取得的ip都是反向代理服务器的ip,取得的域名也是反向代理配置的url的域名,解决该问题,需要在nginx反向代理配置中添加一些配置信息,目的将客户端的真实ip和域名传递 ...
- sock_ntop等函数
inet_ntop的一个基本问题是:它要求调用者传递一个指向某个二进制地址的指针, 而该地址通常包含在一个套接字地址结构中,这就要求调用者必须知道这个结构的格式和地址簇, 为了使用这个函数,我们必须为 ...
- 实验三:gdb跟踪调试内核从start_kernel到init进程启动
原创作品转载请注明出处<Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 如果我写的不好或者有误的地方请留言 ...
- Tomat部署Web运用
在Tomcat部署Web运用的方式主要有如下几种 >利用Tomcat的自动部署 >利用控制台部署 >增加自定义的Web部署文件 >修改server.xml问价部署Web运 ...
- cf C. Bits
http://codeforces.com/contest/485/problem/C 利用位运算来解决这个题,从L开始,从每一位按位或,知道到达r位置,ans=ans|(1<<k);就可 ...
- poj 2892 &&hdu 1540 Tunnel Warfare
http://poj.org/problem?id=2892 #include <cstdio> #include <cstring> #include <algorit ...