Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

auto-refetching.component.ts

46 lines1.1 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 inject,
5 signal,
6} from '@angular/core'
7import {
8 injectMutation,
9 injectQuery,
10} from '@tanstack/angular-query-experimental'
11import { NgStyle } from '@angular/common'
12import { TasksService } from '../services/tasks.service'
13​
14@Component({
15 changeDetection: ChangeDetectionStrategy.OnPush,
16 selector: 'auto-refetching-example',
17 standalone: true,
18 templateUrl: './auto-refetching.component.html',
19 imports: [NgStyle],
20})
21export class AutoRefetchingExampleComponent {
22 #tasksService = inject(TasksService)
23​
24 intervalMs = signal(1000)
25​
26 tasks = injectQuery(() => this.#tasksService.allTasks(this.intervalMs()))
27​
28 addMutation = injectMutation(() => this.#tasksService.addTask())
29 clearMutation = injectMutation(() => this.#tasksService.clearAllTasks())
30​
31 clearTasks() {
32 this.clearMutation.mutate()
33 }
34​
35 inputChange($event: Event) {
36 const target = $event.target as HTMLInputElement
37 this.intervalMs.set(Number(target.value))
38 }
39​
40 addItem($event: Event) {
41 const target = $event.target as HTMLInputElement
42 const value = target.value
43 this.addMutation.mutate(value)
44 target.value = ''
45 }