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