Open History to see the last commit
1
2β
3[](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
4[](https://github.com/TanStack/query/blob/main/LICENSE)
5[](https://bundlephobia.com/package/@tanstack/angular-query-experimental)
6[](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
7β
8# Angular Query
9β
10> IMPORTANT: This library is currently in an experimental stage. This means that breaking changes may 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.
11β
12Functions for fetching, caching and updating asynchronous data in Angular
13β
14# Documentation
15β
16Visit https://tanstack.com/query/latest/docs/framework/angular/overview
17β
18## Quick Features
19β
20- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
21- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
22- Parallel + Dependent Queries
23- Mutations + Reactive Query Refetching
24- Multi-layer Cache + Automatic Garbage Collection
25- Paginated + Cursor-based Queries
26- Load-More + Infinite Scroll Queries w/ Scroll Recovery
27- Request Cancellation
28- Dedicated Devtools (see the [Angular Devtools guide](https://tanstack.com/query/latest/docs/framework/angular/devtools))
29- Built-in SSR dehydration and client hydration when using `provideTanStackQuery` (see the [SSR guide](https://tanstack.com/query/latest/docs/framework/angular/guides/ssr))
30β
31# Quick Start
32β
33> The Angular adapter for TanStack Query requires Angular 19 or higher.
34β
351. Install `angular-query`
36β
37```bash
38$ npm i @tanstack/angular-query-experimental
39```
40β
41or
42β
43```bash
44$ pnpm add @tanstack/angular-query-experimental
45```
46β
47or
48β
49```bash
50$ yarn add @tanstack/angular-query-experimental
51```
52β
53or
54β
55```bash
56$ bun add @tanstack/angular-query-experimental
57```
58β
592. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
60β
61```ts
62import { provideTanStackQuery } from '@tanstack/angular-query-experimental'
63import { QueryClient } from '@tanstack/angular-query-experimental'
64β
65bootstrapApplication(AppComponent, {
66 providers: [provideTanStackQuery(new QueryClient())],
67})
68```
69β
70`provideTanStackQuery` returns a single [`EnvironmentProviders`](https://angular.dev/api/core/EnvironmentProviders) value for **application / environment** config β not for [`@Component({ providers })`](https://angular.dev/api/core/Component#providers).
71β
72or in a NgModule-based app
73β
74```ts
75import { provideHttpClient } from '@angular/common/http'
76import {
77 provideTanStackQuery,
78 QueryClient,
79} from '@tanstack/angular-query-experimental'
80β
81@NgModule({
82 declarations: [AppComponent],
83 imports: [BrowserModule],
84 providers: [provideTanStackQuery(new QueryClient()), provideHttpClient()],
85 bootstrap: [AppComponent],
86})
87export class AppModule {}
88```
89β
903. Inject query
91β
92```ts
93import { injectQuery } from '@tanstack/angular-query-experimental'
94import { Component } from '@angular/core'
95β
96@Component({...})
97export class TodosComponent {
98 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
99}
100```
101β
1024. If you need to update options on your query dynamically, make sure to pass them as signals. The query will refetch automatically if data for an updated query key is stale or not present.
103β
104[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/router)
105β
106```ts
107@Component({})
108export class PostComponent {
109 #postsService = inject(PostsService)
110 postId = input.required({
111 transform: numberAttribute,
112 })
113β
114 postQuery = injectQuery(() => ({
115 queryKey: ['post', this.postId()],
116 queryFn: () => {
117 return lastValueFrom(this.#postsService.postById$(this.postId()))
118 },
119 }))
120}
121β
122@Injectable({
123 providedIn: 'root',
124})
125export class PostsService {
126 #http = inject(HttpClient)
127β
128 postById$ = (postId: number) =>
129 this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
130}
131β
132export interface Post {
133 id: number
134 title: string
135 body: string
136}
137```
138β
139<!-- -->