@tanstack/angular-query-experimental
Version:
Signals for managing, caching and syncing asynchronous and remote data in Angular
136 lines (100 loc) • 3.89 kB
Markdown

[](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
[](https://github.com/TanStack/query/blob/main/LICENSE)
[](https://bundlephobia.com/package/@tanstack/angular-query-experimental)
[](https://www.npmjs.com/package/@tanstack/angular-query-experimental)
# Angular Query
> 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.
Functions for fetching, caching and updating asynchronous data in Angular
# Documentation
Visit https://tanstack.com/query/latest/docs/framework/angular/overview
## Quick Features
- Transport/protocol/backend agnostic data fetching (REST, GraphQL, promises, whatever!)
- Auto Caching + Refetching (stale-while-revalidate, Window Refocus, Polling/Realtime)
- Parallel + Dependent Queries
- Mutations + Reactive Query Refetching
- Multi-layer Cache + Automatic Garbage Collection
- Paginated + Cursor-based Queries
- Load-More + Infinite Scroll Queries w/ Scroll Recovery
- Request Cancellation
- Dedicated Devtools
# Quick Start
> The Angular adapter for TanStack Query requires Angular 16 or higher.
1. Install `angular-query`
```bash
$ npm i /angular-query-experimental
```
or
```bash
$ pnpm add /angular-query-experimental
```
or
```bash
$ yarn add /angular-query-experimental
```
or
```bash
$ bun add /angular-query-experimental
```
2. Initialize **TanStack Query** by adding **provideTanStackQuery** to your application
```ts
import { provideTanStackQuery } from '@tanstack/angular-query-experimental'
import { QueryClient } from '@tanstack/angular-query-experimental'
bootstrapApplication(AppComponent, {
providers: [provideTanStackQuery(new QueryClient())],
})
```
or in a NgModule-based app
```ts
import { provideHttpClient } from '@angular/common/http'
import {
provideTanStackQuery,
QueryClient,
} from '@tanstack/angular-query-experimental'
```
3. Inject query
```ts
import { injectQuery } from '@tanstack/angular-query-experimental'
import { Component } from '@angular/core'
export class TodosComponent {
info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
}
```
4. 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.
[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/router)
```ts
export class PostComponent {
#postsService = inject(PostsService)
postId = input.required({
transform: numberAttribute,
})
postQuery = injectQuery(() => ({
queryKey: ['post', this.postId()],
queryFn: () => {
return lastValueFrom(this.#postsService.postById$(this.postId()))
},
}))
}
export class PostsService {
#http = inject(HttpClient)
postById$ = (postId: number) =>
this.#http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${postId}`)
}
export interface Post {
id: number
title: string
body: string
}
```
<!-- -->