-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathlazy-load-devtools-panel-example.component.ts
52 lines (49 loc) · 1.45 KB
/
lazy-load-devtools-panel-example.component.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import {
ChangeDetectionStrategy,
Component,
Injector,
computed,
inject,
viewChild,
} from '@angular/core'
import { ExampleQueryComponent } from './example-query.component'
import type { ElementRef } from '@angular/core'
import type { DevtoolsPanelRef } from '@tanstack/angular-query-devtools-experimental'
@Component({
selector: 'lazy-load-devtools-panel-example',
changeDetection: ChangeDetectionStrategy.OnPush,
template: `
<example-query />
<h1>Lazy load devtools panel example</h1>
<p>
In this example, the devtools panel is loaded programmatically when the
button is clicked. In addition, the code is lazy loaded.
</p>
<button type="button" (click)="toggleDevtools()">
{{ isOpen ? 'Close' : 'Open' }} the devtools panel
</button>
@if (isOpen) {
<div #div style="height: 500px"></div>
}
`,
imports: [ExampleQueryComponent],
})
export default class LazyLoadDevtoolsPanelExampleComponent {
isOpen = false
devtools?: Promise<DevtoolsPanelRef>
injector = inject(Injector)
divEl = viewChild<ElementRef>('div')
devToolsOptions = computed(() => ({
hostElement: this.divEl(),
}))
toggleDevtools() {
this.isOpen = !this.isOpen
if (!this.devtools) {
this.devtools = import(
'@tanstack/angular-query-devtools-experimental'
).then(({ injectDevtoolsPanel }) =>
injectDevtoolsPanel(this.devToolsOptions, { injector: this.injector }),
)
}
}
}