Skip to content

Commit baf9b19

Browse files
docs(www): fix and update getting started examples (#5058)
1 parent d2c05f7 commit baf9b19

3 files changed

Lines changed: 48 additions & 121 deletions

File tree

projects/www/src/app/examples/store/src/app.component.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
import { Component } from '@angular/core';
2+
// 👇 Import the counter component
23
import { MyCounterComponent } from './my-counter/my-counter.component';
34

45
@Component({
56
selector: 'ngrx-root',
7+
// 👇 Add the counter component to the imports
68
imports: [MyCounterComponent],
79
template: `
810
<h1>NgRx Tutorial</h1>
911
12+
<!-- 👇 add the counter component -->
1013
<ngrx-my-counter></ngrx-my-counter>
1114
`,
1215
})
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { Component } from '@angular/core';
2+
import { Observable } from 'rxjs';
3+
4+
@Component({
5+
selector: 'ngrx-my-counter',
6+
template: `
7+
<button (click)="increment()">Increment</button>
8+
9+
<div>Current Count: {{ count$ | async }}</div>
10+
11+
<button (click)="decrement()">Decrement</button>
12+
13+
<button (click)="reset()">Reset Counter</button>
14+
`,
15+
})
16+
export class MyCounterComponent {
17+
count$: Observable<number>;
18+
19+
constructor() {
20+
// TODO: Connect `this.count$` stream to the current store `count` state
21+
}
22+
23+
increment() {
24+
// TODO: Dispatch an increment action
25+
}
26+
27+
decrement() {
28+
// TODO: Dispatch a decrement action
29+
}
30+
31+
reset() {
32+
// TODO: Dispatch a reset action
33+
}
34+
}

projects/www/src/app/pages/guide/store/index.md

Lines changed: 11 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -39,149 +39,39 @@ The following tutorial shows you how to manage the state of a counter, and how t
3939

4040
1. Generate a new project using the <ngrx-docs-stackblitz name="ngrx-start"></ngrx-docs-stackblitz>.
4141

42-
2. Right click on the `app` folder in StackBlitz and create a new file named `counter.actions.ts` to describe the counter actions to increment, decrement, and reset its value.
42+
2. Right click on the `src` folder in StackBlitz and create a new file named `counter.actions.ts` to describe the counter actions to increment, decrement, and reset its value.
4343

44-
<ngrx-code-example header="src/app/counter.actions.ts" path="store/src/app/counter.actions.ts">
44+
<ngrx-code-example header="src/counter.actions.ts" path="store/src/counter.actions.ts">
4545

4646
</ngrx-code-example>
4747

4848
3. Define a reducer function to handle changes in the counter value based on the provided actions.
4949

50-
<ngrx-code-example header="src/app/counter.reducer.ts" path="store/src/app/counter.reducer.ts">
50+
<ngrx-code-example header="src/counter.reducer.ts" path="store/src/counter.reducer.ts">
5151

5252
</ngrx-code-example>
5353

54-
4. Import the `StoreModule` from `@ngrx/store` and the `counter.reducer` file.
54+
4. Add the `provideStore` function in the `providers` array of your `ApplicationConfig` (within `app.config.ts`) with an object containing the `count` and the `counterReducer` that manages the state of the counter. The `provideStore` method registers the global providers needed to access the `Store` throughout your application.
5555

56-
<ngrx-code-example header="src/app/app.module.ts (imports)" path="store/src/app/app.module.ts" region="imports">
56+
<ngrx-code-example header="src/app.config.ts" path="store/src/app.config.ts">
5757

5858
</ngrx-code-example>
5959

60-
5. Add the `StoreModule.forRoot` function in the `imports` array of your `AppModule` with an object containing the `count` and the `counterReducer` that manages the state of the counter. The `StoreModule.forRoot()` method registers the global providers needed to access the `Store` throughout your application.
60+
5. Create a new file called `my-counter.component.ts` in a folder named `my-counter` within the `app` folder that defines a new component called `MyCounterComponent`. This component renders buttons that allow the user to change the count state. Also add the component's HTML template to the component using the `template` property. If you prefer to use an external HTML file for the template, create a file named `my-counter.component.html` in the same folder and move the template code there, then update the `templateUrl` property of the component accordingly.
6161

62-
<ngrx-code-example header="src/app/app.module.ts (StoreModule)" path="store/src/app/app.module.1.ts">
62+
<ngrx-code-example header="src/my-counter/my-counter.component.todo.ts" path="store/src/my-counter/my-counter.component.todo.ts">
6363

6464
</ngrx-code-example>
6565

66-
6. Create a new file called `my-counter.component.ts` in a folder named `my-counter` within 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. Also, create the `my-counter.component.html` file within this same folder.
66+
6. Add the new component to your AppComponent's imports and declare it in the template:
6767

68-
<ngrx-code-example header="src/app/my-counter/my-counter.component.ts" >
69-
70-
```ts
71-
import { Component } from '@angular/core';
72-
import { Observable } from 'rxjs';
73-
74-
@Component({
75-
selector: 'ngrx-my-counter',
76-
templateUrl: './my-counter.component.html',
77-
})
78-
export class MyCounterComponent {
79-
count$: Observable<number>;
80-
81-
constructor() {
82-
// TODO: Connect `this.count$` stream to the current store `count` state
83-
}
84-
85-
increment() {
86-
// TODO: Dispatch an increment action
87-
}
88-
89-
decrement() {
90-
// TODO: Dispatch a decrement action
91-
}
92-
93-
reset() {
94-
// TODO: Dispatch a reset action
95-
}
96-
}
97-
```
98-
99-
</ngrx-code-example>
100-
101-
<ngrx-code-example header="src/app/my-counter/my-counter.component.html" >
102-
103-
```html
104-
<button (click)="increment()">Increment</button>
105-
106-
<div>Current Count: {{ count$ | async }}</div>
107-
108-
<button (click)="decrement()">Decrement</button>
109-
110-
<button (click)="reset()">Reset Counter</button>
111-
```
68+
<ngrx-code-example header="src/app.component.ts" path="store/src/app.component.ts">
11269

11370
</ngrx-code-example>
11471

115-
7. Add the new component to your AppModule's declarations and declare it in the template:
116-
117-
<ngrx-code-example header="src/app/app.component.html" path="store/src/app/app.component.html" region="counter">
118-
119-
```html
120-
<ngrx-my-counter></ngrx-my-counter>
121-
```
122-
123-
</ngrx-code-example>
124-
125-
<ngrx-code-example header="src/app/app.module.ts" path="store/src/app/app.module.ts">
126-
127-
```ts
128-
import { BrowserModule } from '@angular/platform-browser';
129-
import { NgModule } from '@angular/core';
130-
131-
import { AppComponent } from './app.component';
132-
133-
import { StoreModule } from '@ngrx/store';
134-
import { counterReducer } from './counter.reducer';
135-
import { MyCounterComponent } from './my-counter/my-counter.component';
136-
137-
@NgModule({
138-
declarations: [AppComponent, MyCounterComponent],
139-
imports: [
140-
BrowserModule,
141-
StoreModule.forRoot({ count: counterReducer }),
142-
],
143-
providers: [],
144-
bootstrap: [AppComponent],
145-
})
146-
export class AppModule {}
147-
```
148-
149-
</ngrx-code-example>
150-
151-
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.
152-
153-
<ngrx-code-example header="src/app/my-counter/my-counter.component.ts" path="store/src/app/my-counter/my-counter.component.ts">
154-
155-
```ts
156-
import { Component } from '@angular/core';
157-
import { Store } from '@ngrx/store';
158-
import { Observable } from 'rxjs';
159-
import { increment, decrement, reset } from '../counter.actions';
160-
161-
@Component({
162-
selector: 'ngrx-my-counter',
163-
templateUrl: './my-counter.component.html',
164-
})
165-
export class MyCounterComponent {
166-
count$: Observable<number>;
167-
168-
constructor(private store: Store<{ count: number }>) {
169-
this.count$ = store.select('count');
170-
}
171-
172-
increment() {
173-
this.store.dispatch(increment());
174-
}
175-
176-
decrement() {
177-
this.store.dispatch(decrement());
178-
}
72+
7. 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.
17973

180-
reset() {
181-
this.store.dispatch(reset());
182-
}
183-
}
184-
```
74+
<ngrx-code-example header="src/my-counter/my-counter.component.ts" path="store/src/my-counter/my-counter.component.ts">
18575

18676
</ngrx-code-example>
18777

0 commit comments

Comments
 (0)