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

README.md

129 lines3.4 KB

1<picture>
2 <source
3 media="(prefers-color-scheme: dark)"
4 srcset="https://tanstack.com/api/readme/query.png?framework=angular&theme=dark"
5 />
6 <source
7 media="(prefers-color-scheme: light)"
8 srcset="https://tanstack.com/api/readme/query.png?framework=angular"
9 />
10 <img
11 src="https://tanstack.com/api/readme/query.png?framework=angular"
12 alt="TanStack Angular Query"
13 width="900"
14 />
15</picture>
16​
17[![npm version](https://img.shields.io/npm/v/@tanstack/angular-query)](https://www.npmjs.com/package/@tanstack/angular-query)
18[![npm license](https://img.shields.io/npm/l/@tanstack/angular-query)](https://github.com/TanStack/query/blob/main/LICENSE)
19[![bundle size](https://img.shields.io/bundlephobia/minzip/@tanstack/angular-query)](https://bundlephobia.com/package/@tanstack/angular-query)
20[![npm](https://img.shields.io/npm/dm/@tanstack/angular-query)](https://www.npmjs.com/package/@tanstack/angular-query)
21​
22# Angular Query
23​
24Functions for fetching, caching and updating asynchronous data in Angular
25​
26# Documentation
27​
28Visit https://tanstack.com/query/latest/docs/framework/angular/overview
29​
30## Quick Features
31​
32- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
33- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
34- Parallel + Dependent Queries
35- Mutations + Reactive Query Refetching
36- Multi-layer Cache + Automatic Garbage Collection
37- Paginated + Cursor-based Queries
38- Load-More + Infinite Scroll Queries w/ Scroll Recovery
39- Request Cancellation
40- Dedicated Devtools
41​
42# Quick Start
43​
44> The Angular adapter for TanStack Query requires Angular 20.1 or higher.
45​
461. Install `angular-query`
47​
48```bash
49$ npm i @tanstack/angular-query
50```
51​
52or
53​
54```bash
55$ pnpm add @tanstack/angular-query
56```
57​
58or
59​
60```bash
61$ yarn add @tanstack/angular-query
62```
63​
64or
65​
66```bash
67$ bun add @tanstack/angular-query
68```
69​
702. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
71​
72Pass a factory; Angular runs it once per injector and in an injection context.
73​
74```ts
75import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'
76​
77bootstrapApplication(AppComponent, {
78 providers: [provideTanStackQuery(() => new QueryClient())],
79})
80```
81​
823. Inject query
83​
84```ts
85import { injectQuery } from '@tanstack/angular-query'
86import { Component } from '@angular/core'
87​
88@Component({...})
89export class TodosComponent {
90 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
92```
93​
944. 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.
95​
96[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/basic-angular-router)
97​
98```ts
99@Component({})
100export class PostComponent {
101 #postsService = inject(PostsService)
102 postId = input.required({
103 transform: numberAttribute,
104 })
105​
106 postQuery = injectQuery(() => ({
107 queryKey: ['post', this.postId()],
108 queryFn: () => {
109 return lastValueFrom(this.#postsService.postById$(this.postId()))
110 },
111 }))
113​
114@Injectable({
115 providedIn: 'root',
116})
117export class PostsService {
118 #http = inject(HttpClient)
119​
120 postById$ = (postId: number) =>
121 this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
123​
124export interface Post {
125 id: number
126 title: string
127 body: string
129```