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