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

provideTanStackQuery.md

94 lines2.8 KB

1---
2id: provideTanStackQuery
3title: provideTanStackQuery
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9function provideTanStackQuery(queryClientFactory: () => QueryClient, ...features: readonly QueryFeature[]): EnvironmentProviders;
10```
11​
12Defined in: [packages/angular-query/src/providers.ts:126](https://github.com/TanStack/query/blob/main/packages/angular-query/src/providers.ts#L126)
13​
14Sets up providers necessary to enable TanStack Query functionality for Angular applications. Provides a
15`QueryClient` and optional TanStack Query features. The factory runs once per injector in Angular's
16injection context, so it can call `inject()` and each SSR request can receive an independent cache.
17​
18## Parameters
19​
20### queryClientFactory
21​
22() => [`QueryClient`](../classes/QueryClient.md)
23​
24Creates or resolves a `QueryClient` in the injection context.
25​
26### features
27​
28...readonly [`QueryFeature`](../interfaces/QueryFeature.md)[]
29​
30Optional features to configure additional Query functionality.
31​
32## Returns
33​
34`EnvironmentProviders`
35​
36A single EnvironmentProviders value (do not spread into `providers`).
37​
38## See
39​
40 - https://tanstack.com/query/v5/docs/framework/angular/quick-start
41 - https://tanstack.com/query/v5/docs/framework/angular/devtools
42 - https://tanstack.com/query/latest/docs/framework/angular/guides/ssr
43​
44## Examples
45​
46```ts
47import {
48 provideTanStackQuery,
49 QueryClient,
50} from '@tanstack/angular-query'
51​
52bootstrapApplication(AppComponent, {
53 providers: [provideTanStackQuery(() => new QueryClient())],
54})
55```
56​
57The same, in an `NgModule`-based application:
58```ts
59import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'
60​
61@NgModule({
62 declarations: [AppComponent],
63 imports: [BrowserModule],
64 providers: [provideTanStackQuery(() => new QueryClient())],
65 bootstrap: [AppComponent],
66})
67export class AppModule {}
68```
69​
70Enabling optional developer tools by adding `withDevtools` β€” by default, the tools are then loaded when
71your app is in development mode:
72```ts
73import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query'
74import { withDevtools } from '@tanstack/angular-query-devtools'
75​
76bootstrapApplication(AppComponent, {
77 providers: [
78 provideTanStackQuery(() => new QueryClient(), withDevtools()),
79 ],
80})
81```
82​
83Resolving the `QueryClient` from an `InjectionToken` β€” an advanced optimization that lets TanStack Query
84be absent from the main application bundle, useful for including it on lazy-loaded routes only while
85still sharing a `QueryClient`. This is a small optimization; for most applications it's preferable to
86provide the `QueryClient` in the main application config, as in the examples above:
87```ts
88export const MY_QUERY_CLIENT = new InjectionToken('', {
89 factory: () => new QueryClient(),
90})
91​
92// In a lazy loaded route's providers array:
93providers: [provideTanStackQuery(() => inject(MY_QUERY_CLIENT))]
94```