StoreModule.ProvideStore
import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; import { FormsModule } from '@angular/forms'; @NgModule({ declarations: [ AppComponent ], imports: [ BrowserModule, FormsModule, ], providers: [], bootstrap: [AppComponent] }) export class AppModule { }
StoreModule.provideStore({myReducer}),
export const myReducer: ActionReducer<AppState> = (state = intitialState, action: Action) => { ... }
export interface AppState { user:User, bullets: Bullet[] };
export interface User { name: string xPos: number yPos: number }; export interface Bullet { xPos: number, yPos: number, shotBy: string };
export const intitialState: AppState = { user: {name: 'Jim', xPos: 0, yPos: 0}, bullets: [] };
That's It!
constructor(private http:Http, private store:Store<GlobalState>) { this.store.select(s => s.myReducer.user) .subscribe( (data:any)=> { console.log('app component got reducer\'s state: ' + data); }); this.store.dispatch({type:INCREMENT}); }
export interface GlobalState { myReducer: AppState }