From 19a79d4c939f3855a3ae06ea9e4a7304d7004119 Mon Sep 17 00:00:00 2001 From: Paul Touchton Date: Mon, 20 Jul 2020 19:45:27 -0700 Subject: [PATCH] docs(store): Refined step 6,7, and 8 of getting started guide to be more detailed. closes #1880 --- projects/ngrx.io/content/guide/store/index.md | 54 ++++++++++++++++--- 1 file changed, 46 insertions(+), 8 deletions(-) diff --git a/projects/ngrx.io/content/guide/store/index.md b/projects/ngrx.io/content/guide/store/index.md index 03f8756175..6b3430c59a 100644 --- a/projects/ngrx.io/content/guide/store/index.md +++ b/projects/ngrx.io/content/guide/store/index.md @@ -46,21 +46,59 @@ The following tutorial shows you how to manage the state of a counter, and how t -6. Create a new _Component_ named `my-counter` in the `app` folder. Inject the `Store` service into your component to dispatch the counter actions, and use the `select` operator to _select_ data from the state. +6. Create a new file called `my-counter.component.ts` in the `app` folder that will define a new component called `MyCounterComponent`. This component will render buttons that allow the user to change the count state. + + +import { Component } from '@angular/core'; +import { Store, select } from '@ngrx/store'; +import { Observable } from 'rxjs'; +import { increment, decrement, reset } from '../counter.actions'; + +@Component({ + selector: 'app-my-counter', + template: ` + <button (click)="increment()">Increment</button> + + <div>Current Count: {{ count$ | async }}</div> + + <button (click)="decrement()">Decrement</button> + + <button (click)="reset()">Reset Counter</button> + ` +}) +export class MyCounterComponent { + count$: Observable<number> + + constructor(private store: Store<{ count: number }>) { + // TODO: This stream will connect to the current store `count` state + this.count$ = store.pipe(select('count')); + } + + increment() { + // TODO: Dispatch an increment action + } + + decrement() { + // TODO: Dispatch a decrement action + } + + reset() { + // TODO: Dispatch a reset action + } +} + -Update the `MyCounterComponent` template with buttons to call the increment, decrement, and reset methods. Use the async pipe to subscribe to the _count$_ observable. +7. Add the new component to your AppModule's declarations and declare it in the template: - + -Update the `MyCounterComponent` class with a selector for the _count_, and methods to dispatch the Increment, Decrement, and Reset actions. - - + -7. Add the `MyCounter` component to your `AppComponent` template. +8. Inject the store into `MyCounterComponent` and connect the `count$` stream to the store's `count` state. Implement the `increment`, `decrement`, and `reset` methods by dispatching actions to the store. - + And that's it! Click the increment, decrement, and reset buttons to change the state of the counter.