[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: 1}),
            Observable.interval(1000).mapTo({type: SECOND, payload: 1})
            )
            .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
* */
时间: 2024-10-05 13:49:56

[Angular 2] Dispatching Action with Payloads and type to Reducers的相关文章

[Angular] NgRx/effect, why to use it?

See the current implementaion of code, we have a smart component, and inside the smart component we are using both 'serivce' and 'store'. In the large application, what we really want is one service to handle the application state instead of two or m

angularJS (2) angular.min.js

angular.min.js /* AngularJS v1.2.29 (c) 2010-2014 Google, Inc. http://angularjs.org License: MIT*/(function(V,W,v){'use strict';function z(b){return function(){var a=arguments[0],c,a="["+(b?b+":":"")+a+"] http://errors.a

angular初步认识一

最近比较流行MVC前端框架开发,最近研究了一个框架AngularJS框架 不说那么多,先上例子,我是个代码控 <!DOCTYPE html> <html lang="en" ng-app="myApp"> <head> <meta charset="UTF-8"> <title>capter1-angular</title> <link rel="styles

angular js 实现表单提交时下面的table获取到表单里面的数据

angular js 实现表单提交时下面的table获取到表单里面的数据<!DOCTYPE html><html ><head lang="en"> <meta charset="UTF-8"> <title></title> <link rel="stylesheet" href="css/bootstrap.min.css"/> <s

[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 response, you need to dispatch action to tell the store update. So one pattern can be considered to follow is: import {Http, Headers} from '@angular/http';

angular学习笔记(五)-阶乘计算实例(3)

同样是上一个例子,我们要求并非实时的计算结果,而是等到用户确定自己已经输入完毕,然后进行计算: <!DOCTYPE html> <html ng-app> <head> <title>2.3.4计算阶乘实例3</title> <meta charset="utf-8"> <script src="../angular.js"></script> <script sr

angular笔记_7

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Document</title> <script src="http://cdn.static.runoob.com/libs/angular.js/1.4.6/angular.min.js"></script> &l

struts2在配置文件中调用Action的方法返回值

struts2在配置文件中可以调用Action的方法返回值 1.Action中 //文件下载名 public String getDownloadFileName(){ String downloadFileName = ""; String filename = fileName + ".xls"; try { downloadFileName = URLEncoder.encode(filename,"UTF-8"); } catch (Un

Struts2—Action

二.命名空间namespace ·命名空间namespace须要放在相应的package下 ·Namespace必须以"/开头". ·Result的name是"success"的<result>能够不写其name.即: <result name="success">与<result>效果同样. ·Namespace为空的情形是: 当找到url找到相应的namespace下的action时,假设资源没有找到.那么