[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="0">
        <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(1000)
        .mapTo({type: SECOND, payload: 1});

    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))
    }
}
时间: 2024-08-05 07:40:39

[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 passing them into your Subject.next() call. This lesson shows you how to make a number input and pass the value so you can configure how much you want the

[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 state reducers handle this problem by allowing the parent component to override any state change. In Short, we want to have a way to override the compo

[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 c

[Angular 2] Using a Reducer to Change an Object&#39;s Property Inside an Array

Reducers are also often used for changing a single property inside of other reducers. This lesson shows how a type can enter the people reducer, but then the people reducer can use a different type to call the clock reducer and get a value back. So t

Angular vs React 最全面深入对比

如今,Angular和React这两个JavaScript框架可谓红的发紫,同时针对这两个框架的选择变成了当下最容易被问及或者被架构设计者考虑的问题,本文或许无法告诉你哪个框架更优秀,但尽量从更多的角度去比较两者,尽可能的为你在选择时提供更多的参考意见. 选择的方法 在选择之前,我们尝试带着一些问题去审视你将要选择的框架(或者是任何工具),尝试用这些问题的答案来帮助我们更加了解框架,也更加让选择变得更容易 框架本身的问题: 是否成熟?谁在背后支持呢? 具备的功能? 采用什么架构和模式? 生态系统

手把手教你用ngrx管理Angular状态

本文将与你一起探讨如何用不可变数据储存的方式进行Angular应用的状态管理 :ngrx/store--Angular的响应式Redux.本文将会完成一个小型简单的Angular应用,最终代码可以在这里下载. Angular应用中的状态管理 近几年,大型复杂Angular/AngularJS项目的状态管理一直是个让人头疼的问题.在AngularJS(1.x版本)中,状态管理通常由服务,事件,$rootScope混合处理.在Angular中(2+版本),组件通信让状态管理变得清晰一些,但还是有点复

angular开发者吐槽react+redux的复杂:“一个demo证明你的开发效率低下”

曾经看到一篇文章,写的是jquery开发者吐槽angular的复杂.作为一个angular开发者,我来吐槽一下react+redux的复杂. 例子 为了让大家看得舒服,我用最简单的一个demo来展示react+redux的“弯弯绕”,下面这个程序就是我用react和redux写的.然而这个程序在angular中一行js都不用写!!! 展示组件 App.js import React, { findDOMNode, Component } from 'react'; import ReactDOM

NgRx/Store 4 + Angular 5使用教程

这篇文章将会示范如何使用NgRx/Store 4和Angular5.@ngrx/store是基于RxJS的状态管理库,其灵感来源于Redux.在NgRx中,状态是由一个包含action和reducer的函数的映射组成的.Reducer函数经由action的分发以及当前或初始的状态而被调用,最后由reducer返回一个不可变的状态.你可以在@ngrx/store中找到我们将在下面的例子中讨论到的相关的API. Action: Action是状态的改变.它描述了某个事件的发生,但是没有指定应用的状态

angular 2+ 路由守卫

1. 定义接口名称 /domain/login-guard.ts export interface LoginGuard { data: any; msg: string; status: boolean; } 2. 定义actions  /ngrx/actions/login-guard.action.ts import { Action } from '@ngrx/store'; import {LoginGuard} from '../../domain/login-guard'; /**