import { AsyncPipe } from '@angular/common';
import {
ChangeDetectionStrategy,
Component,
EventEmitter,
Input,
Output,
} from '@angular/core';
import { LetDirective } from '@ngrx/component';
import { ComponentStore } from '@ngrx/component-store';
@Component({
selector: 'app-counter',
standalone: true,
imports: [AsyncPipe, LetDirective],
template: `
Counter: {{ counter }}
`,
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class CounterComponent extends ComponentStore<{ counter: number }> {
@Input() set initialValue(initialValue: number) {
this.patchState({ counter: initialValue });
}
@Output() send = new EventEmitter();
readonly counter$ = this.select((state) => state.counter);
readonly increment = this.updater((state) => ({
counter: state.counter + 1,
}));
readonly decrement = this.updater((state) => ({
counter: state.counter - 1,
}));
constructor() {
super({ counter: 0 });
}
}