|
1 | | -import { AsyncPipe } from '@angular/common'; |
2 | 1 | import { |
3 | 2 | ChangeDetectionStrategy, |
4 | 3 | Component, |
5 | | - EventEmitter, |
6 | | - Input, |
7 | | - Output, |
| 4 | + input, |
| 5 | + linkedSignal, |
| 6 | + output, |
8 | 7 | } from '@angular/core'; |
9 | | -import { LetDirective } from '@ngrx/component'; |
10 | | -import { ComponentStore } from '@ngrx/component-store'; |
11 | 8 |
|
12 | 9 | @Component({ |
13 | 10 | selector: 'app-counter', |
14 | | - imports: [AsyncPipe, LetDirective], |
15 | 11 | template: ` |
16 | | - <ng-container *ngrxLet="counter$ as counter"> |
17 | | - <p data-testid="counter">Counter: {{ counter }}</p> |
18 | | - <button (click)="increment()">Increment</button> |
19 | | - <button (click)="decrement()">Decrement</button> |
20 | | - <button (click)="send.emit(counter)">Send</button> |
21 | | - </ng-container> |
| 12 | + <p data-testid="counter">Counter: {{ counter() }}</p> |
| 13 | + <button (click)="increment()">Increment</button> |
| 14 | + <button (click)="decrement()">Decrement</button> |
| 15 | + <button (click)="send.emit(counter())">Send</button> |
22 | 16 | `, |
23 | 17 | changeDetection: ChangeDetectionStrategy.OnPush, |
24 | 18 | }) |
25 | | -export class CounterComponent extends ComponentStore<{ counter: number }> { |
26 | | - @Input() set initialValue(initialValue: number) { |
27 | | - this.patchState({ counter: initialValue }); |
28 | | - } |
| 19 | +export class CounterComponent { |
| 20 | + initialValue = input.required<number>(); |
| 21 | + public counter = linkedSignal(() => this.initialValue()); |
29 | 22 |
|
30 | | - @Output() send = new EventEmitter<number>(); |
| 23 | + send = output<number>(); |
31 | 24 |
|
32 | | - readonly counter$ = this.select((state) => state.counter); |
| 25 | + public increment = () => { |
| 26 | + this.counter.set(this.counter() + 1); |
| 27 | + }; |
33 | 28 |
|
34 | | - readonly increment = this.updater((state) => ({ |
35 | | - counter: state.counter + 1, |
36 | | - })); |
37 | | - readonly decrement = this.updater((state) => ({ |
38 | | - counter: state.counter - 1, |
39 | | - })); |
40 | | - |
41 | | - constructor() { |
42 | | - super({ counter: 0 }); |
43 | | - } |
| 29 | + public decrement = () => { |
| 30 | + this.counter.set(this.counter() - 1); |
| 31 | + }; |
44 | 32 | } |
0 commit comments