Open History to see the last commit
1import {
2 ChangeDetectionStrategy,
3 Component,
4 Injector,
5 computed,
6 inject,
7 viewChild,
8} from '@angular/core'
9import { ExampleQueryComponent } from './example-query.component'
10import type { ElementRef } from '@angular/core'
11import type { DevtoolsPanelRef } from '@tanstack/angular-query-devtools-experimental'
12β
13@Component({
14 standalone: true,
15 selector: 'lazy-load-devtools-panel-example',
16 changeDetection: ChangeDetectionStrategy.OnPush,
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)="toggleDevtools()">
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 isOpen = false
35 devtools?: Promise<DevtoolsPanelRef>
36 injector = inject(Injector)
37β
38 divEl = viewChild<ElementRef>('div')
39 devToolsOptions = computed(() => ({
40 hostElement: this.divEl(),
41 }))
42β
43 toggleDevtools() {
44 this.isOpen = !this.isOpen
45 if (!this.devtools) {
46 this.devtools = import(
47 '@tanstack/angular-query-devtools-experimental'
48 ).then(({ injectDevtoolsPanel }) =>
49 injectDevtoolsPanel(this.devToolsOptions, this.injector),
50 )
51 }
52 }
53}