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

provideTanStackQuery.md

90 lines2.6 KB

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