Open History to see the last commit
1import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
2import {
3 ChangeDetectionStrategy,
4 Component,
5 EventEmitter,
6 Input,
7 Output,
8 signal,
9} from '@angular/core'
10import { CommonModule, NgIf } from '@angular/common'
11import {
12 injectQuery,
13 injectQueryClient,
14} from '@tanstack/angular-query-experimental'
15β
16type Post = {
17 id: number
18 title: string
19 body: string
20}
21β
22@Component({
23 changeDetection: ChangeDetectionStrategy.OnPush,
24 selector: 'post',
25 standalone: true,
26 imports: [NgIf],
27 template: `
28 <div>
29 <div>
30 <a (click)="setPostId.emit(-1)" href="#"> Back </a>
31 </div>
32 <ng-container *ngIf="postQuery.status() === 'pending'">
33 Loading...
34 </ng-container>
35 <ng-container *ngIf="postQuery.status() === 'error'">
36 Error: {{ postQuery.error()?.message }}
37 </ng-container>
38 <ng-container *ngIf="postQuery.data() as post">
39 <h1>{{ post.title }}</h1>
40 <div>
41 <p>{{ post.body }}</p>
42 </div>
43 <div *ngIf="postQuery.isFetching()">Background Updating...</div>
44 </ng-container>
45 </div>
46 `,
47})
48export class PostComponent {
49 @Output() setPostId = new EventEmitter<number>()
50β
51 // Until Angular supports signal-based inputs, we have to set a signal
52 @Input({ required: true })
53 set postId(value: number) {
54 this.postIdSignal.set(value)
55 }
56 postIdSignal = signal<number>(0)
57β
58 // You can even leave out the queryFn and just go straight into options
59 postQuery = injectQuery<Post>(() => ({
60 enabled: this.postIdSignal() > 0,
61 queryKey: [`/posts/${this.postIdSignal()}`],
62 }))
63β
64 queryClient = injectQueryClient
65}
66β
67@Component({
68 changeDetection: ChangeDetectionStrategy.OnPush,
69 selector: 'posts',
70 standalone: true,
71 template: `<div>
72 <h1>Posts</h1>
73 <div [ngSwitch]="postsQuery.status()">
74 <div *ngSwitchCase="'pending'">Loading...</div>
75 <div *ngSwitchCase="'error'">
76 Error: {{ postsQuery.error()?.message }}
77 </div>
78 <ng-container *ngSwitchDefault>
79 <p *ngFor="let post of postsQuery.data()">
80 <!-- We can access the query data here to show bold links for-->
81 <!-- ones that are cached-->
82 <a
83 href="#"
84 (click)="setPostId.emit(post.id)"
85 [style]="
86 queryClient.getQueryData(['/posts/', post.id])
87 ? {
88 fontWeight: 'bold',
89 color: 'green'
90 }
91 : {}
92 "
93 >{{ post.title }}</a
94 >
95 </p>
96 </ng-container>
97β
98 <div *ngIf="postsQuery.isFetching()">Background Updating...</div>
99 </div>
100 </div>`,
101 imports: [CommonModule],
102})
103export class PostsComponent {
104 @Output() setPostId = new EventEmitter<number>()
105β
106 // All you have to do now is pass a key!
107 postsQuery = injectQuery<Array<Post>>(() => ({
108 queryKey: ['/posts'],
109 }))
110β
111 queryClient = injectQueryClient()
112}
113β
114@Component({
115 changeDetection: ChangeDetectionStrategy.OnPush,
116 selector: 'default-query-function-example',
117 standalone: true,
118 template: `
119 <p>
120 As you visit the posts below, you will notice them in a loading state the
121 first time you load them. However, after you return to this list and click
122 on any posts you have already visited again, you will see them load
123 instantly and background refresh right before your eyes!
124 <strong>
125 (You may need to throttle your network speed to simulate longer loading
126 sequences)
127 </strong>
128 </p>
129 <angular-query-devtools initialIsOpen />
130 <div *ngIf="postId() > -1; else posts">
131 <post [postId]="postId()" (setPostId)="postId.set($event)"></post>
132 </div>
133 <ng-template #posts>
134 <posts (setPostId)="postId.set($event)" />
135 </ng-template>
136 `,
137 imports: [AngularQueryDevtools, NgIf, PostComponent, PostsComponent],
138})
139export class DefaultQueryFunctionExampleComponent {
140 postId = signal(-1)
141}