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

lazy-load-devtools-panel-example.component.ts

59 lines1.6 KB

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