diff --git a/docs/config.json b/docs/config.json index ad0177b53c..ffe9ec23a4 100644 --- a/docs/config.json +++ b/docs/config.json @@ -1043,7 +1043,7 @@ "to": "framework/angular/examples/pagination" }, { - "label": "Infinite query with Max pages", + "label": "Infinite query with maxPages", "to": "framework/angular/examples/infinite-query-with-max-pages" }, { @@ -1057,6 +1057,10 @@ { "label": "Query options from a service", "to": "framework/angular/examples/query-options-from-a-service" + }, + { + "label": "Devtools embedded panel", + "to": "framework/angular/examples/devtools-panel" } ] } diff --git a/examples/angular/devtools-panel/src/app/app.config.ts b/examples/angular/devtools-panel/src/app/app.config.ts index c5b827e588..b51d14cb81 100644 --- a/examples/angular/devtools-panel/src/app/app.config.ts +++ b/examples/angular/devtools-panel/src/app/app.config.ts @@ -1,10 +1,17 @@ import { provideHttpClient, withFetch } from '@angular/common/http' import { provideRouter } from '@angular/router' +import { + QueryClient, + provideTanStackQuery, +} from '@tanstack/angular-query-experimental' import { routes } from './app.routes' import type { ApplicationConfig } from '@angular/core' export const appConfig: ApplicationConfig = { - // In this example, `provideTanStackQuery` is used in the router - providers: [provideHttpClient(withFetch()), provideRouter(routes)], + providers: [ + provideHttpClient(withFetch()), + provideRouter(routes), + provideTanStackQuery(new QueryClient()), + ], } diff --git a/examples/angular/devtools-panel/src/app/app.routes.ts b/examples/angular/devtools-panel/src/app/app.routes.ts index b423235506..2b1b1e6765 100644 --- a/examples/angular/devtools-panel/src/app/app.routes.ts +++ b/examples/angular/devtools-panel/src/app/app.routes.ts @@ -1,7 +1,3 @@ -import { - QueryClient, - provideTanStackQuery, -} from '@tanstack/angular-query-experimental' import type { Route } from '@angular/router' export const routes: Array = [ @@ -14,12 +10,10 @@ export const routes: Array = [ path: 'basic', loadComponent: () => import('./components/basic-devtools-panel-example.component'), - providers: [provideTanStackQuery(new QueryClient())], }, { path: 'lazy', loadComponent: () => import('./components/lazy-load-devtools-panel-example.component'), - providers: [provideTanStackQuery(new QueryClient())], }, ] diff --git a/examples/angular/devtools-panel/src/app/components/basic-devtools-panel-example.component.ts b/examples/angular/devtools-panel/src/app/components/basic-devtools-panel-example.component.ts index c3fcbbead0..e54fc1b2d7 100644 --- a/examples/angular/devtools-panel/src/app/components/basic-devtools-panel-example.component.ts +++ b/examples/angular/devtools-panel/src/app/components/basic-devtools-panel-example.component.ts @@ -1,9 +1,4 @@ -import { - ChangeDetectionStrategy, - Component, - signal, - viewChild, -} from '@angular/core' +import { ChangeDetectionStrategy, Component, viewChild } from '@angular/core' import { injectDevtoolsPanel } from '@tanstack/angular-query-devtools-experimental' import { ExampleQueryComponent } from './example-query.component' import type { ElementRef } from '@angular/core' @@ -19,24 +14,19 @@ import type { ElementRef } from '@angular/core' In this example, the devtools panel is loaded programmatically when the button is clicked

- - @if (isOpen()) { + @if (isOpen) {
} `, - imports: [ExampleQueryComponent], }) export default class BasicDevtoolsPanelExampleComponent { - isOpen = signal(false) + isOpen = false divEl = viewChild('div') - toggleDevtools() { - this.isOpen.update((prev) => !prev) - } - devtools = injectDevtoolsPanel(() => ({ hostElement: this.divEl(), })) diff --git a/examples/angular/devtools-panel/src/app/components/lazy-load-devtools-panel-example.component.ts b/examples/angular/devtools-panel/src/app/components/lazy-load-devtools-panel-example.component.ts index efacbff414..675f9b67c8 100644 --- a/examples/angular/devtools-panel/src/app/components/lazy-load-devtools-panel-example.component.ts +++ b/examples/angular/devtools-panel/src/app/components/lazy-load-devtools-panel-example.component.ts @@ -2,9 +2,8 @@ import { ChangeDetectionStrategy, Component, Injector, - effect, + computed, inject, - signal, viewChild, } from '@angular/core' import { ExampleQueryComponent } from './example-query.component' @@ -20,48 +19,35 @@ import type { DevtoolsPanelRef } from '@tanstack/angular-query-devtools-experime

Lazy load devtools panel example

In this example, the devtools panel is loaded programmatically when the - button is clicked. In addition, the code is lazy loaded + button is clicked. In addition, the code is lazy loaded.

- @if (isOpen()) { + @if (isOpen) {
} `, imports: [ExampleQueryComponent], }) export default class LazyLoadDevtoolsPanelExampleComponent { - isOpen = signal(false) - divEl = viewChild('div') - devtools?: DevtoolsPanelRef + isOpen = false + devtools?: Promise injector = inject(Injector) - toggleDevtools() { - this.isOpen.update((prev) => !prev) - } - - constructor() { - effect(() => { - const isOpen = this.isOpen() - if (!isOpen || this.devtools) return - void this.lazyInitDevtools() - }) - } + divEl = viewChild('div') + devToolsOptions = computed(() => ({ + hostElement: this.divEl(), + })) - async lazyInitDevtools() { - // As the import is dynamic, it will not be included in the main bundle - // and will be lazy loaded only when the button is clicked - // Instead of a button you could also define a keyboard shortcut to - // load the devtools panel on demand - const { injectDevtoolsPanel } = await import( - '@tanstack/angular-query-devtools-experimental' - ) - this.devtools = injectDevtoolsPanel( - () => ({ - hostElement: this.divEl(), - }), - this.injector, - ) + toggleDevtools() { + this.isOpen = !this.isOpen + if (!this.devtools) { + this.devtools = import( + '@tanstack/angular-query-devtools-experimental' + ).then(({ injectDevtoolsPanel }) => + injectDevtoolsPanel(this.devToolsOptions, this.injector), + ) + } } } diff --git a/examples/angular/query-options-from-a-service/src/app/services/posts-service.ts b/examples/angular/query-options-from-a-service/src/app/services/posts-service.ts deleted file mode 100644 index fed2a1b11c..0000000000 --- a/examples/angular/query-options-from-a-service/src/app/services/posts-service.ts +++ /dev/null @@ -1,21 +0,0 @@ -import { HttpClient } from '@angular/common/http' -import { Injectable, inject } from '@angular/core' - -@Injectable({ - providedIn: 'root', -}) -export class PostsService { - #http = inject(HttpClient) - - postById$ = (postId: number) => - this.#http.get(`https://jsonplaceholder.typicode.com/posts/${postId}`) - - allPosts$ = () => - this.#http.get>('https://jsonplaceholder.typicode.com/posts') -} - -export interface Post { - id: number - title: string - body: string -} diff --git a/examples/angular/query-options-from-a-service/src/app/services/queries-service.ts b/examples/angular/query-options-from-a-service/src/app/services/queries-service.ts index dd2cca982d..e05477805f 100644 --- a/examples/angular/query-options-from-a-service/src/app/services/queries-service.ts +++ b/examples/angular/query-options-from-a-service/src/app/services/queries-service.ts @@ -1,19 +1,29 @@ import { Injectable, inject } from '@angular/core' import { lastValueFrom } from 'rxjs' import { queryOptions } from '@tanstack/angular-query-experimental' -import { PostsService } from './posts-service' +import { HttpClient } from '@angular/common/http' + +export interface Post { + id: number + title: string + body: string +} @Injectable({ providedIn: 'root', }) export class QueriesService { - private postsService = inject(PostsService) + private http = inject(HttpClient) post(postId: number) { return queryOptions({ queryKey: ['post', postId], queryFn: () => { - return lastValueFrom(this.postsService.postById$(postId)) + return lastValueFrom( + this.http.get( + `https://jsonplaceholder.typicode.com/posts/${postId}`, + ), + ) }, }) } @@ -21,7 +31,12 @@ export class QueriesService { posts() { return queryOptions({ queryKey: ['posts'], - queryFn: () => lastValueFrom(this.postsService.allPosts$()), + queryFn: () => + lastValueFrom( + this.http.get>( + 'https://jsonplaceholder.typicode.com/posts', + ), + ), }) } }