mirror of
https://github.com/Raghu-Ch/angular-challenges.git
synced 2026-02-10 04:43:03 -05:00
feat(project): update 19
This commit is contained in:
@@ -5,7 +5,7 @@ import { CounterComponent } from './counter.component';
|
|||||||
imports: [CounterComponent],
|
imports: [CounterComponent],
|
||||||
selector: 'app-root',
|
selector: 'app-root',
|
||||||
template: `
|
template: `
|
||||||
<app-counter [initialValue]="10" (send)="log($event)"></app-counter>
|
<app-counter [initialValue]="10" (send)="log($event)" />
|
||||||
`,
|
`,
|
||||||
})
|
})
|
||||||
export class AppComponent {
|
export class AppComponent {
|
||||||
|
|||||||
@@ -1,44 +1,32 @@
|
|||||||
import { AsyncPipe } from '@angular/common';
|
|
||||||
import {
|
import {
|
||||||
ChangeDetectionStrategy,
|
ChangeDetectionStrategy,
|
||||||
Component,
|
Component,
|
||||||
EventEmitter,
|
input,
|
||||||
Input,
|
linkedSignal,
|
||||||
Output,
|
output,
|
||||||
} from '@angular/core';
|
} from '@angular/core';
|
||||||
import { LetDirective } from '@ngrx/component';
|
|
||||||
import { ComponentStore } from '@ngrx/component-store';
|
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-counter',
|
selector: 'app-counter',
|
||||||
imports: [AsyncPipe, LetDirective],
|
|
||||||
template: `
|
template: `
|
||||||
<ng-container *ngrxLet="counter$ as counter">
|
<p data-testid="counter">Counter: {{ counter() }}</p>
|
||||||
<p data-testid="counter">Counter: {{ counter }}</p>
|
|
||||||
<button (click)="increment()">Increment</button>
|
<button (click)="increment()">Increment</button>
|
||||||
<button (click)="decrement()">Decrement</button>
|
<button (click)="decrement()">Decrement</button>
|
||||||
<button (click)="send.emit(counter)">Send</button>
|
<button (click)="send.emit(counter())">Send</button>
|
||||||
</ng-container>
|
|
||||||
`,
|
`,
|
||||||
changeDetection: ChangeDetectionStrategy.OnPush,
|
changeDetection: ChangeDetectionStrategy.OnPush,
|
||||||
})
|
})
|
||||||
export class CounterComponent extends ComponentStore<{ counter: number }> {
|
export class CounterComponent {
|
||||||
@Input() set initialValue(initialValue: number) {
|
initialValue = input.required<number>();
|
||||||
this.patchState({ counter: initialValue });
|
public counter = linkedSignal(() => this.initialValue());
|
||||||
}
|
|
||||||
|
send = output<number>();
|
||||||
@Output() send = new EventEmitter<number>();
|
|
||||||
|
public increment = () => {
|
||||||
readonly counter$ = this.select((state) => state.counter);
|
this.counter.set(this.counter() + 1);
|
||||||
|
};
|
||||||
readonly increment = this.updater((state) => ({
|
|
||||||
counter: state.counter + 1,
|
public decrement = () => {
|
||||||
}));
|
this.counter.set(this.counter() - 1);
|
||||||
readonly decrement = this.updater((state) => ({
|
};
|
||||||
counter: state.counter - 1,
|
|
||||||
}));
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
super({ counter: 0 });
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user