Skip to content

Instantly share code, notes, and snippets.

/action.ts Secret

Created May 4, 2017 16:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4416276ee42e42c019dfafab0fedef8a to your computer and use it in GitHub Desktop.
Save anonymous/4416276ee42e42c019dfafab0fedef8a to your computer and use it in GitHub Desktop.
import { Action } from '@ngrx/store';
export const INCREMENT = '[Counter] Increment';
export const DECREMENT = '[Counter] Decrement';
export class CounterActions {
static readonly INCREMENT = INCREMENT;
static readonly DECREMENT = DECREMENT;
increment(): Action {
return {
type: CounterActions.INCREMENT,
payload: 1
}
}
decrement(): Action {
return {
type: CounterActions.DECREMENT,
payload: 1
}
}
}
import { Component } from '@angular/core';
import { Store } from '@ngrx/store';
import {Observable} from "rxjs/Observable";
interface AppState {
counter: number;
}
import {CounterActions} from '../../actions/counter';
@Component({
selector: 'page-home',
templateUrl: 'home.html'
})
export class HomePage
{
counter: Observable<number>;
constructor(private store: Store<AppState>, public counterActions: CounterActions){
this.counter = store.select('counter');
}
increment(){
this.store.dispatch(this.counterActions.increment());
}
decrement(){
this.store.dispatch(this.counterActions.decrement());
}
}
import { INCREMENT, DECREMENT } from '../actions/counter';
const INITIAL_STATE: number = 0;
export function counterReducer(state: number = INITIAL_STATE, action: any)
{
console.log(action);
switch(action.type)
{
case INCREMENT:
return state + action.payload;
case DECREMENT:
return state - action.payload;
default:
return state;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment