Open History to see the last commit
1---
2id: quick-start
3title: Quick Start
4---
5β
6> IMPORTANT: This library is currently in an experimental stage. This means that breaking changes will happen in minor AND patch releases. Upgrade carefully. If you use this in production while in experimental stage, please lock your version to a patch-level version to avoid unexpected breaking changes.
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 provideTanStackQuery,
18 QueryClient,
19} from '@tanstack/angular-query-experimental'
20β
21bootstrapApplication(AppComponent, {
22 providers: [provideHttpClient(), provideTanStackQuery(new QueryClient())],
23})
24```
25β
26or in a NgModule-based app
27β
28```ts
29import { provideHttpClient } from '@angular/common/http'
30import {
31 provideTanStackQuery,
32 QueryClient,
33} from '@tanstack/angular-query-experimental'
34β
35@NgModule({
36 declarations: [AppComponent],
37 imports: [BrowserModule],
38 providers: [provideTanStackQuery(new QueryClient())],
39 bootstrap: [AppComponent],
40})
41export class AppModule {}
42```
43β
44### Component with query and mutation
45β
46```angular-ts
47import { Component, Injectable, inject } from '@angular/core'
48import { HttpClient } from '@angular/common/http'
49import { lastValueFrom } from 'rxjs'
50β
51import {
52 injectMutation,
53 injectQuery,
54 QueryClient
55} from '@tanstack/angular-query-experimental'
56β
57@Component({
58 standalone: true,
59 template: `
60 <div>
61 <button (click)="onAddTodo()">Add Todo</button>
62β
63 <ul>
64 @for (todo of query.data(); track todo.title) {
65 <li>{{ todo.title }}</li>
66 }
67 </ul>
68 </div>
69 `,
70})
71export class TodosComponent {
72 todoService = inject(TodoService)
73 queryClient = inject(QueryClient)
74β
75 query = injectQuery(() => ({
76 queryKey: ['todos'],
77 queryFn: () => this.todoService.getTodos(),
78 }))
79β
80 mutation = injectMutation(() => ({
81 mutationFn: (todo: Todo) => this.todoService.addTodo(todo),
82 onSuccess: () => {
83 this.queryClient.invalidateQueries({ queryKey: ['todos'] })
84 },
85 }))
86β
87 onAddTodo() {
88 this.mutation.mutate({
89 id: Date.now().toString(),
90 title: 'Do Laundry',
91 })
92 }
93}
94β
95@Injectable({ providedIn: 'root' })
96export class TodoService {
97 private http = inject(HttpClient)
98β
99 getTodos(): Promise<Todo[]> {
100 return lastValueFrom(
101 this.http.get<Todo[]>('https://jsonplaceholder.typicode.com/todos'),
102 )
103 }
104β
105 addTodo(todo: Todo): Promise<Todo> {
106 return lastValueFrom(
107 this.http.post<Todo>('https://jsonplaceholder.typicode.com/todos', todo),
108 )
109 }
110}
111β
112interface Todo {
113 id: string
114 title: string
115}
116```
117β
118[//]: # 'Example'