Open History to see the last commit
1import {
2 ChangeDetectionStrategy,
3 Component,
4 Injector,
5 computed,
6 effect,
7 inject,
8 runInInjectionContext,
9 signal,
10 viewChild,
11} from '@angular/core'
12import { ExampleQueryComponent } from './example-query.component'
13import type { ElementRef } from '@angular/core'
14import type { DevtoolsPanelRef } from '@tanstack/angular-query-devtools/devtools-panel'
15β
16@Component({
17 selector: 'lazy-load-devtools-panel-example',
18 changeDetection: ChangeDetectionStrategy.OnPush,
19 template: `
20 <example-query />
21 <h1>Lazy load devtools panel example</h1>
22 <p>
23 In this example, the devtools panel is loaded programmatically when the
24 button is clicked. In addition, the code is lazy loaded.
25 </p>
26 <button type="button" (click)="toggleIsOpen()">
27 {{ isOpen() ? 'Close' : 'Open' }} the devtools panel
28 </button>
29 @if (isOpen()) {
30 <div #div style="height: 500px"></div>
31 }
32 `,
33 imports: [ExampleQueryComponent],
34})
35export default class LazyLoadDevtoolsPanelExampleComponent {
36 readonly isOpen = signal(false)
37 readonly devtools = signal<Promise<DevtoolsPanelRef> | undefined>(undefined)
38 readonly injector = inject(Injector)
39β
40 readonly divEl = viewChild<ElementRef>('div')
41 readonly devToolsOptions = computed(() => ({
42 hostElement: this.divEl(),
43 }))
44β
45 toggleIsOpen() {
46 this.isOpen.update((prev) => !prev)
47 }
48β
49 readonly loadDevtoolsEffect = effect(() => {
50 if (this.devtools()) return
51 if (this.isOpen()) {
52 this.devtools.set(
53 import('@tanstack/angular-query-devtools/devtools-panel').then(
54 ({ injectDevtoolsPanel }) =>
55 runInInjectionContext(this.injector, () =>
56 injectDevtoolsPanel(this.devToolsOptions),
57 ),
58 ),
59 )
60 }
61 })
62}