博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[Angular 2] Dispatching Action with Payloads and type to Reducers
阅读量:6236 次
发布时间:2019-06-22

本文共 1990 字,大约阅读时间需要 6 分钟。

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.tsexport 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: `                

{
{clock | async | date:'yMMMMEEEEdjms'}}

`})export class App { click$ = new Subject(); clock; constructor(store:Store) { this.clock = store.select('clock'); Observable.merge( this.click$.mapTo({type: HOUR, payload: 1}), Observable.interval(1000).mapTo({type: SECOND, payload: 1}) ) .subscribe((action)=>{ store.dispatch(action) }) }}

 

// main.tsimport {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* */

 

转载地址:http://ouzia.baihongyu.com/

你可能感兴趣的文章
oracle在线迁移同步数据,数据库报错
查看>>
linux性能剖析工具
查看>>
flutter中的异步
查看>>
计算机高手也不能编出俄罗斯方块——计算机达人成长之路(16)
查看>>
error LNK2001: 无法解析的外部符号 __CrtDbgReport
查看>>
【多线程】的简单理解&进程 and【你的电脑是几核的?】
查看>>
# 2017-2018-1 20155224 《信息安全系统设计基础》第七周学习总结
查看>>
scikit-learn预处理实例之一:使用FunctionTransformer选择列
查看>>
【距离GDOI:137天】 扩展KMP...字符串QAQ
查看>>
Oracle 10g 下载地址
查看>>
c# Unity依赖注入WebService
查看>>
邮件客户端导入邮件通讯录地址薄
查看>>
java中异常抛出后代码还会继续执行吗
查看>>
oracle 学习摘记
查看>>
IOS代码布局(一) UIView
查看>>
如何解决开机出现Missing operating system的故障
查看>>
Android AudioPolicyService服务启动过程
查看>>
SVG的a链接
查看>>
MSSQL查找前一天,前一月,前一年的数据,对比当前时间记录查找超过一年,一月,一天的数据...
查看>>
基于三星I9250演示自己弄的Miracast功能-手机对手机
查看>>