11import {
22 DestroyRef ,
33 ENVIRONMENT_INITIALIZER ,
4- Injector ,
54 PLATFORM_ID ,
65 computed ,
76 effect ,
87 inject ,
98 makeEnvironmentProviders ,
10- runInInjectionContext ,
119} from '@angular/core'
1210import { QueryClient , onlineManager } from '@tanstack/query-core'
1311import { isPlatformBrowser } from '@angular/common'
@@ -99,7 +97,7 @@ export function provideTanStackQuery(
9997 return makeEnvironmentProviders ( [
10098 provideQueryClient ( queryClient ) ,
10199 {
102- // Do not use provideEnvironmentInitializer to support Angular < v19
100+ // Do not use provideEnvironmentInitializer while Angular < v19 is supported
103101 provide : ENVIRONMENT_INITIALIZER ,
104102 multi : true ,
105103 useValue : ( ) => {
@@ -250,14 +248,17 @@ export function withDevtools(
250248 } else {
251249 providers = [
252250 {
251+ // Do not use provideEnvironmentInitializer while Angular < v19 is supported
253252 provide : ENVIRONMENT_INITIALIZER ,
254253 multi : true ,
255254 useFactory : ( ) => {
256255 if ( ! isPlatformBrowser ( inject ( PLATFORM_ID ) ) ) return noop
257- const injector = inject ( Injector )
258- const options = computed ( ( ) =>
259- runInInjectionContext ( injector , ( ) => optionsFn ?.( ) ?? { } ) ,
260- )
256+ const injectedClient = inject ( QueryClient , {
257+ optional : true ,
258+ } )
259+ const destroyRef = inject ( DestroyRef )
260+
261+ const options = computed ( ( ) => optionsFn ?.( ) ?? { } )
261262
262263 let devtools : TanstackQueryDevtools | null = null
263264 let el : HTMLElement | null = null
@@ -269,10 +270,7 @@ export function withDevtools(
269270 : isDevMode ( )
270271 } )
271272
272- const destroyRef = inject ( DestroyRef )
273-
274273 const getResolvedQueryClient = ( ) => {
275- const injectedClient = injector . get ( QueryClient , null )
276274 const client = options ( ) . client ?? injectedClient
277275 if ( ! client ) {
278276 throw new Error ( 'No QueryClient found' )
@@ -314,22 +312,20 @@ export function withDevtools(
314312 el = document . body . appendChild ( document . createElement ( 'div' ) )
315313 el . classList . add ( 'tsqd-parent-container' )
316314
317- import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) =>
318- runInInjectionContext ( injector , ( ) => {
319- devtools = new queryDevtools . TanstackQueryDevtools ( {
320- ...options ( ) ,
321- client : getResolvedQueryClient ( ) ,
322- queryFlavor : 'Angular Query' ,
323- version : '5' ,
324- onlineManager,
325- } )
315+ import ( '@tanstack/query-devtools' ) . then ( ( queryDevtools ) => {
316+ devtools = new queryDevtools . TanstackQueryDevtools ( {
317+ ...options ( ) ,
318+ client : getResolvedQueryClient ( ) ,
319+ queryFlavor : 'Angular Query' ,
320+ version : '5' ,
321+ onlineManager,
322+ } )
326323
327- el && devtools . mount ( el )
324+ el && devtools . mount ( el )
328325
329- // Unmount the devtools on application destroy
330- destroyRef . onDestroy ( destroyDevtools )
331- } ) ,
332- )
326+ // Unmount the devtools on application destroy
327+ destroyRef . onDestroy ( destroyDevtools )
328+ } )
333329 } )
334330 } ,
335331 } ,
0 commit comments