Open History to see the last commit
1---
2id: quick-start
3title: Quick Start
4---
5β
6> VERY IMPORTANT: This library is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Use at your own risk. If you choose to rely on this in production in an experimental stage, please lock your version to a patch-level version to avoid unexpected breakages.
7β
8[//]: # 'Example'
9β
10If you're looking for a fully functioning example, please have a look at our [basic codesandbox example](./examples/basic)
11β
12### Provide the client to your App
13β
14```ts
15import { provideHttpClient } from '@angular/common/http'
16import {
17 provideAngularQuery,
18 QueryClient,
19} from '@tanstack/angular-query-experimental'
20β
21bootstrapApplication(AppComponent, {
22 providers: [provideHttpClient(), provideAngularQuery(new QueryClient())],
23})
24```
25β
26### Component with query and mutation
27β
28```ts
29import { Component, Injectable, inject } from '@angular/core'
30import { HttpClient } from '@angular/common/http'
31import { lastValueFrom } from 'rxjs'
32β
33import {
34 injectMutation,
35 injectQuery,
36 injectQueryClient,
37} from '@tanstack/angular-query-experimental'
38β
39@Component({
40 standalone: true,
41 template: `
42 <div>
43 <button (click)="onAddTodo()">Add Todo</button>
44β
45 <ul>
46 @for (todo of query.data(); track todo.title) {
47 <li>{{ todo.title }}</li>
48 }
49 </ul>
50 </div>
51 `,
52})
53export class TodosComponent {
54 todoService = inject(TodoService)
55 queryClient = injectQueryClient()
56β
57 query = injectQuery(() => ({
58 queryKey: ['todos'],
59 queryFn: () => this.todoService.getTodos(),
60 }))
61β
62 mutation = injectMutation((client) => ({
63 mutationFn: (todo: Todo) => this.todoService.addTodo(todo),
64 onSuccess: () => {
65 // Invalidate and refetch by using the client directly
66 client.invalidateQueries({ queryKey: ['todos'] })
67β
68 // OR use the queryClient that is injected into the component
69 // this.queryClient.invalidateQueries({ queryKey: ['todos'] })
70 },
71 }))
72β
73 onAddTodo() {
74 this.mutation.mutate({
75 id: Date.now().toString(),
76 title: 'Do Laundry',
77 })
78 }
79}
80β
81@Injectable({ providedIn: 'root' })
82export class TodoService {
83 private http = inject(HttpClient)
84β
85 getTodos(): Promise<Todo[]> {
86 return lastValueFrom(
87 this.http.get<Todo[]>('https://jsonplaceholder.typicode.com/todos'),
88 )
89 }
90β
91 addTodo(todo: Todo): Promise<Todo> {
92 return lastValueFrom(
93 this.http.post<Todo>('https://jsonplaceholder.typicode.com/todos', todo),
94 )
95 }
96}
97β
98interface Todo {
99 id: string
100 title: string
101}
102```
103β
104[//]: # 'Example'