web-dave / angular-starter-v2

6 stars 3 forks source link

install and use @ngrx/store #31

Open web-dave opened 7 years ago

web-dave commented 7 years ago
web-dave commented 7 years ago
npm i -S @ngrx/store 
npm i -D @ngrx/store-devtools
web-dave commented 7 years ago

./books/store/books.reducer.ts

import { Action } from "@ngrx/store";

import { IBook } from "../shared/custom-types";

export const booksStoreName = "books";

export interface BooksState {
  books: IBook[];
}

export const initialState: BooksState = {
  books: []
};

export function booksReducer(state = initialState, action: Action): BooksState {
   return state
}
web-dave commented 7 years ago

app.module.ts

import { StoreModule } from "@ngrx/store";
import { StoreDevtoolsModule } from "@ngrx/store-devtools";

...
@NgModule({
 declarations: [...],
 imports: [
   ...,
   StoreModule.forRoot({}),
   StoreDevtoolsModule.instrument({ maxAge: 100 })
 ],
 ...
})
export class AppModule {
...
}
web-dave commented 6 years ago

books.module.ts

import { StoreModule } from "@ngrx/store";

...
@NgModule({
 declarations: [...],
 imports: [
   ...,
    StoreModule.forFeature(booksStoreName, {
      books: booksReducer
    })
 ],
 ...
})
export class BooksModule {
...
}