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

README.md

139 lines4.4 KB

1![TanStack Query Header](https://github.com/TanStack/query/raw/main/media/repo-header.png)
2​
3[![npm version](https://img.shields.io/npm/v/@tanstack/angular-query-experimental)](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
4[![npm license](https://img.shields.io/npm/l/@tanstack/angular-query-experimental)](https://github.com/TanStack/query/blob/main/LICENSE)
5[![bundle size](https://img.shields.io/bundlephobia/minzip/@tanstack/angular-query-experimental)](https://bundlephobia.com/package/@tanstack/angular-query-experimental)
6[![npm](https://img.shields.io/npm/dm/@tanstack/angular-query-experimental)](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 }))
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 }))
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}`)
131​
132export interface Post {
133 id: number
134 title: string
135 body: string
137```
138​
139<!-- -->