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

provideTanStackQuery.md

99 lines2.7 KB

1---
2id: provideTanStackQuery
3title: provideTanStackQuery
4---
5​
6# Function: provideTanStackQuery()
7​
8```ts
9function provideTanStackQuery(queryClient, ...features): Provider[];
10```
11​
12Defined in: [providers.ts:151](https://github.com/TanStack/query/blob/main/packages/angular-query-experimental/src/providers.ts#L151)
13​
14Sets up providers necessary to enable TanStack Query functionality for Angular applications.
15​
16Allows to configure a `QueryClient` and optional features such as developer tools.
17​
18**Example - standalone**
19​
20```ts
21import {
22 provideTanStackQuery,
23 QueryClient,
24} from '@tanstack/angular-query-experimental'
25​
26bootstrapApplication(AppComponent, {
27 providers: [...provideTanStackQuery(new QueryClient())],
28})
29```
30​
31**Example - NgModule-based**
32​
33```ts
34import {
35 provideTanStackQuery,
36 QueryClient,
37} from '@tanstack/angular-query-experimental'
38​
39@NgModule({
40 declarations: [AppComponent],
41 imports: [BrowserModule],
42 providers: [...provideTanStackQuery(new QueryClient())],
43 bootstrap: [AppComponent],
44})
45export class AppModule {}
46```
47​
48You can also enable optional developer tools by adding `withDevtools`. By
49default the tools will then be loaded when your app is in development mode.
50​
51```ts
52import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'
53import { withDevtools } from '@tanstack/angular-query-devtools'
54​
55bootstrapApplication(AppComponent, {
56 providers: [...provideTanStackQuery(new QueryClient(), withDevtools())],
57})
58```
59​
60**Example: using an InjectionToken**
61​
62```ts
63export const MY_QUERY_CLIENT = new InjectionToken('', {
64 factory: () => new QueryClient(),
65})
66​
67// In a lazy loaded route or lazy loaded component's providers array:
68providers: [...provideTanStackQuery(MY_QUERY_CLIENT)]
69```
70Using an InjectionToken for the QueryClient is an advanced optimization which allows TanStack Query to be absent from the main application bundle.
71This can be beneficial if you want to include TanStack Query on lazy loaded routes only while still sharing a `QueryClient`.
72​
73Note that this is a small optimization and for most applications it's preferable to provide the `QueryClient` in the main application config.
74​
75## Parameters
76​
77### queryClient
78​
79A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.
80​
81`QueryClient` | `InjectionToken`\<`QueryClient`\>
82​
83### features
84​
85...[`QueryFeatures`](../type-aliases/QueryFeatures.md)[]
86​
87Optional features to configure additional Query functionality.
88​
89## Returns
90​
91`Provider`[]
92​
93A set of providers to set up TanStack Query (spread into `providers`).
94​
95## See
96​
97 - https://tanstack.com/query/v5/docs/framework/angular/quick-start
98 - https://tanstack.com/query/v5/docs/framework/angular/devtools
99 - https://tanstack.com/query/latest/docs/framework/angular/guides/ssr