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

auto-refetching.component.ts

46 lines1.2 KB

1import {
2 ChangeDetectionStrategy,
3 Component,
4 inject,
5 signal,
6} from '@angular/core'
7import { injectMutation, injectQuery } from '@tanstack/angular-query'
8import { NgStyle } from '@angular/common'
9import { TasksService } from '../services/tasks.service'
10​
11@Component({
12 changeDetection: ChangeDetectionStrategy.OnPush,
13 selector: 'auto-refetching-example',
14 templateUrl: './auto-refetching.component.html',
15 imports: [NgStyle],
16})
17export class AutoRefetchingExampleComponent {
18 readonly #tasksService = inject(TasksService)
19​
20 readonly intervalMs = signal(1000)
21​
22 readonly tasks = injectQuery(() =>
23 this.#tasksService.allTasks(this.intervalMs()),
24 )
25​
26 readonly addMutation = injectMutation(() => this.#tasksService.addTask())
27 readonly clearMutation = injectMutation(() =>
28 this.#tasksService.clearAllTasks(),
29 )
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 }