Open History to see the last commit
1---
2id: quick-start
3title: Quick Start
4---
5β
6[//]: # 'Example'
7β
8If you're looking for a fully functioning example, please have a look at our [basic codesandbox example](./examples/basic)
9β
10### Configure the QueryClient
11β
12Pass a factory to `provideTanStackQuery`. Angular runs it once per injector and in an injection context, so it can use `inject()`.
13β
14```ts
15import { provideHttpClient } from '@angular/common/http'
16import { bootstrapApplication } from '@angular/platform-browser'
17import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'
18β
19bootstrapApplication(AppComponent, {
20 providers: [
21 provideHttpClient(),
22 provideTanStackQuery(() => new QueryClient()),
23 ],
24})
25```
26β
27### Component with query and mutation
28β
29```angular-ts
30import { Component, Injectable, inject } from '@angular/core'
31import { HttpClient } from '@angular/common/http'
32import { lastValueFrom } from 'rxjs'
33β
34import {
35 injectMutation,
36 injectQuery,
37 QueryClient,
38} from '@tanstack/angular-query'
39β
40@Component({
41 template: `
42 <div>
43 <button (click)="onAddTodo()">Add Todo</button>
44β
45 <ul>
46 @for (todo of todosQuery.data(); track todo.title) {
47 <li>{{ todo.title }}</li>
48 }
49 </ul>
50 </div>
51 `,
52})
53export class TodosComponent {
54 readonly todoService = inject(TodoService)
55 readonly queryClient = inject(QueryClient)
56β
57 readonly todosQuery = injectQuery(() => ({
58 queryKey: ['todos'],
59 queryFn: () => this.todoService.getTodos(),
60 }))
61β
62 readonly addTodoMutation = injectMutation(() => ({
63 mutationFn: (todo: Todo) => this.todoService.addTodo(todo),
64 onSuccess: () => {
65 this.queryClient.invalidateQueries({ queryKey: ['todos'] })
66 },
67 }))
68β
69 onAddTodo() {
70 this.addTodoMutation.mutate({
71 id: Date.now().toString(),
72 title: 'Do Laundry',
73 })
74 }
75}
76β
77@Injectable({ providedIn: 'root' })
78export class TodoService {
79 private readonly http = inject(HttpClient)
80β
81 getTodos(): Promise<Todo[]> {
82 return lastValueFrom(
83 this.http.get<Todo[]>('https://jsonplaceholder.typicode.com/todos'),
84 )
85 }
86β
87 addTodo(todo: Todo): Promise<Todo> {
88 return lastValueFrom(
89 this.http.post<Todo>('https://jsonplaceholder.typicode.com/todos', todo),
90 )
91 }
92}
93β
94interface Todo {
95 id: string
96 title: string
97}
98```
99β
100[//]: # 'Example'