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

README.md

136 lines3.8 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
29​
30# Quick Start
31​
32> The Angular adapter for TanStack Query requires Angular 19 or higher.
33​
341. Install `angular-query`
35​
36```bash
37$ npm i @tanstack/angular-query-experimental
38```
39​
40or
41​
42```bash
43$ pnpm add @tanstack/angular-query-experimental
44```
45​
46or
47​
48```bash
49$ yarn add @tanstack/angular-query-experimental
50```
51​
52or
53​
54```bash
55$ bun add @tanstack/angular-query-experimental
56```
57​
582. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
59​
60```ts
61import { provideTanStackQuery } from '@tanstack/angular-query-experimental'
62import { QueryClient } from '@tanstack/angular-query-experimental'
63​
64bootstrapApplication(AppComponent, {
65 providers: [provideTanStackQuery(new QueryClient())],
66})
67```
68​
69or in a NgModule-based app
70​
71```ts
72import { provideHttpClient } from '@angular/common/http'
73import {
74 provideTanStackQuery,
75 QueryClient,
76} from '@tanstack/angular-query-experimental'
77​
78@NgModule({
79 declarations: [AppComponent],
80 imports: [BrowserModule],
81 providers: [provideTanStackQuery(new QueryClient()), provideHttpClient()],
82 bootstrap: [AppComponent],
83})
84export class AppModule {}
85```
86​
873. Inject query
88​
89```ts
90import { injectQuery } from '@tanstack/angular-query-experimental'
91import { Component } from '@angular/core'
92​
93@Component({...})
94export class TodosComponent {
95 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
97```
98​
994. 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.
100​
101[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/router)
102​
103```ts
104@Component({})
105export class PostComponent {
106 #postsService = inject(PostsService)
107 postId = input.required({
108 transform: numberAttribute,
109 })
110​
111 postQuery = injectQuery(() => ({
112 queryKey: ['post', this.postId()],
113 queryFn: () => {
114 return lastValueFrom(this.#postsService.postById$(this.postId()))
115 },
116 }))
118​
119@Injectable({
120 providedIn: 'root',
121})
122export class PostsService {
123 #http = inject(HttpClient)
124​
125 postById$ = (postId: number) =>
126 this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
128​
129export interface Post {
130 id: number
131 title: string
132 body: string
134```
135​
136<!-- -->