Skip to content

Commit f56fc84

Browse files
authored
docs(angular-query): improve examples (TanStack#8273)
1 parent 74c65cc commit f56fc84

File tree

7 files changed

+57
-82
lines changed

7 files changed

+57
-82
lines changed

docs/config.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,7 @@
10431043
"to": "framework/angular/examples/pagination"
10441044
},
10451045
{
1046-
"label": "Infinite query with Max pages",
1046+
"label": "Infinite query with maxPages",
10471047
"to": "framework/angular/examples/infinite-query-with-max-pages"
10481048
},
10491049
{
@@ -1057,6 +1057,10 @@
10571057
{
10581058
"label": "Query options from a service",
10591059
"to": "framework/angular/examples/query-options-from-a-service"
1060+
},
1061+
{
1062+
"label": "Devtools embedded panel",
1063+
"to": "framework/angular/examples/devtools-panel"
10601064
}
10611065
]
10621066
}
Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
import { provideHttpClient, withFetch } from '@angular/common/http'
22

33
import { provideRouter } from '@angular/router'
4+
import {
5+
QueryClient,
6+
provideTanStackQuery,
7+
} from '@tanstack/angular-query-experimental'
48
import { routes } from './app.routes'
59
import type { ApplicationConfig } from '@angular/core'
610

711
export const appConfig: ApplicationConfig = {
8-
// In this example, `provideTanStackQuery` is used in the router
9-
providers: [provideHttpClient(withFetch()), provideRouter(routes)],
12+
providers: [
13+
provideHttpClient(withFetch()),
14+
provideRouter(routes),
15+
provideTanStackQuery(new QueryClient()),
16+
],
1017
}
Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import {
2-
QueryClient,
3-
provideTanStackQuery,
4-
} from '@tanstack/angular-query-experimental'
51
import type { Route } from '@angular/router'
62

73
export const routes: Array<Route> = [
@@ -14,12 +10,10 @@ export const routes: Array<Route> = [
1410
path: 'basic',
1511
loadComponent: () =>
1612
import('./components/basic-devtools-panel-example.component'),
17-
providers: [provideTanStackQuery(new QueryClient())],
1813
},
1914
{
2015
path: 'lazy',
2116
loadComponent: () =>
2217
import('./components/lazy-load-devtools-panel-example.component'),
23-
providers: [provideTanStackQuery(new QueryClient())],
2418
},
2519
]

examples/angular/devtools-panel/src/app/components/basic-devtools-panel-example.component.ts

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
ChangeDetectionStrategy,
3-
Component,
4-
signal,
5-
viewChild,
6-
} from '@angular/core'
1+
import { ChangeDetectionStrategy, Component, viewChild } from '@angular/core'
72
import { injectDevtoolsPanel } from '@tanstack/angular-query-devtools-experimental'
83
import { ExampleQueryComponent } from './example-query.component'
94
import type { ElementRef } from '@angular/core'
@@ -19,24 +14,19 @@ import type { ElementRef } from '@angular/core'
1914
In this example, the devtools panel is loaded programmatically when the
2015
button is clicked
2116
</p>
22-
<button type="button" (click)="toggleDevtools()">
23-
{{ isOpen() ? 'Close' : 'Open' }} the devtools panel
17+
<button type="button" (click)="isOpen = !isOpen">
18+
{{ isOpen ? 'Close' : 'Open' }} the devtools panel
2419
</button>
25-
@if (isOpen()) {
20+
@if (isOpen) {
2621
<div #div style="height: 500px"></div>
2722
}
2823
`,
29-
3024
imports: [ExampleQueryComponent],
3125
})
3226
export default class BasicDevtoolsPanelExampleComponent {
33-
isOpen = signal(false)
27+
isOpen = false
3428
divEl = viewChild<ElementRef>('div')
3529

36-
toggleDevtools() {
37-
this.isOpen.update((prev) => !prev)
38-
}
39-
4030
devtools = injectDevtoolsPanel(() => ({
4131
hostElement: this.divEl(),
4232
}))

examples/angular/devtools-panel/src/app/components/lazy-load-devtools-panel-example.component.ts

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import {
22
ChangeDetectionStrategy,
33
Component,
44
Injector,
5-
effect,
5+
computed,
66
inject,
7-
signal,
87
viewChild,
98
} from '@angular/core'
109
import { ExampleQueryComponent } from './example-query.component'
@@ -20,48 +19,35 @@ import type { DevtoolsPanelRef } from '@tanstack/angular-query-devtools-experime
2019
<h1>Lazy load devtools panel example</h1>
2120
<p>
2221
In this example, the devtools panel is loaded programmatically when the
23-
button is clicked. In addition, the code is lazy loaded
22+
button is clicked. In addition, the code is lazy loaded.
2423
</p>
2524
<button type="button" (click)="toggleDevtools()">
26-
{{ isOpen() ? 'Close' : 'Open' }} the devtools panel
25+
{{ isOpen ? 'Close' : 'Open' }} the devtools panel
2726
</button>
28-
@if (isOpen()) {
27+
@if (isOpen) {
2928
<div #div style="height: 500px"></div>
3029
}
3130
`,
3231
imports: [ExampleQueryComponent],
3332
})
3433
export default class LazyLoadDevtoolsPanelExampleComponent {
35-
isOpen = signal(false)
36-
divEl = viewChild<ElementRef>('div')
37-
devtools?: DevtoolsPanelRef
34+
isOpen = false
35+
devtools?: Promise<DevtoolsPanelRef>
3836
injector = inject(Injector)
3937

40-
toggleDevtools() {
41-
this.isOpen.update((prev) => !prev)
42-
}
43-
44-
constructor() {
45-
effect(() => {
46-
const isOpen = this.isOpen()
47-
if (!isOpen || this.devtools) return
48-
void this.lazyInitDevtools()
49-
})
50-
}
38+
divEl = viewChild<ElementRef>('div')
39+
devToolsOptions = computed(() => ({
40+
hostElement: this.divEl(),
41+
}))
5142

52-
async lazyInitDevtools() {
53-
// As the import is dynamic, it will not be included in the main bundle
54-
// and will be lazy loaded only when the button is clicked
55-
// Instead of a button you could also define a keyboard shortcut to
56-
// load the devtools panel on demand
57-
const { injectDevtoolsPanel } = await import(
58-
'@tanstack/angular-query-devtools-experimental'
59-
)
60-
this.devtools = injectDevtoolsPanel(
61-
() => ({
62-
hostElement: this.divEl(),
63-
}),
64-
this.injector,
65-
)
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+
}
6652
}
6753
}

examples/angular/query-options-from-a-service/src/app/services/posts-service.ts

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,42 @@
11
import { Injectable, inject } from '@angular/core'
22
import { lastValueFrom } from 'rxjs'
33
import { queryOptions } from '@tanstack/angular-query-experimental'
4-
import { PostsService } from './posts-service'
4+
import { HttpClient } from '@angular/common/http'
5+
6+
export interface Post {
7+
id: number
8+
title: string
9+
body: string
10+
}
511

612
@Injectable({
713
providedIn: 'root',
814
})
915
export class QueriesService {
10-
private postsService = inject(PostsService)
16+
private http = inject(HttpClient)
1117

1218
post(postId: number) {
1319
return queryOptions({
1420
queryKey: ['post', postId],
1521
queryFn: () => {
16-
return lastValueFrom(this.postsService.postById$(postId))
22+
return lastValueFrom(
23+
this.http.get<Post>(
24+
`https://jsonplaceholder.typicode.com/posts/${postId}`,
25+
),
26+
)
1727
},
1828
})
1929
}
2030

2131
posts() {
2232
return queryOptions({
2333
queryKey: ['posts'],
24-
queryFn: () => lastValueFrom(this.postsService.allPosts$()),
34+
queryFn: () =>
35+
lastValueFrom(
36+
this.http.get<Array<Post>>(
37+
'https://jsonplaceholder.typicode.com/posts',
38+
),
39+
),
2540
})
2641
}
2742
}

0 commit comments

Comments
 (0)