-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy path06-with-ngrx-store.ts
39 lines (34 loc) · 1013 Bytes
/
06-with-ngrx-store.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { AsyncPipe } from '@angular/common';
import { Component } from '@angular/core';
import { createSelector, Store, createAction, createReducer, on, select } from '@ngrx/store';
const increment = createAction('increment');
const decrement = createAction('decrement');
export const reducer = createReducer(
0,
on(increment, (state) => state + 1),
on(decrement, (state) => state - 1),
);
const selectValue = createSelector(
(state: any) => state.value,
(value) => value * 10,
);
@Component({
standalone: true,
imports: [AsyncPipe],
selector: 'atl-fixture',
template: `
<button (click)="decrement()">Decrement</button>
<span data-testid="value">{{ value | async }}</span>
<button (click)="increment()">Increment</button>
`,
})
export class WithNgRxStoreComponent {
value = this.store.pipe(select(selectValue));
constructor(private store: Store<any>) {}
increment() {
this.store.dispatch(increment());
}
decrement() {
this.store.dispatch(decrement());
}
}