Open History to see the last commit
1---
2id: queries
3title: Queries
4ref: docs/framework/react/guides/queries.md
5replace:
6 {
7 'React': 'Angular',
8 'react-query': 'angular-query',
9 'promise': 'promise or observable',
10 'custom hooks': 'services',
11 'the `useQuery` hook': '`injectQuery`',
12 '`useQuery`': '`injectQuery`',
13 "TypeScript will also narrow the type of data correctly if you've checked for pending and error before accessing it.": 'TypeScript will only narrow the type when checking boolean signals such as `isPending` and `isError`.',
14 }
15---
16β
17[//]: # 'Example'
18β
19```ts
20import { injectQuery } from '@tanstack/angular-query-experimental'
21β
22export class TodosComponent {
23 info = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
24}
25```
26β
27[//]: # 'Example'
28[//]: # 'Example2'
29β
30```ts
31result = injectQuery(() => ({ queryKey: ['todos'], queryFn: fetchTodoList }))
32```
33β
34[//]: # 'Example2'
35[//]: # 'Example3'
36β
37```angular-ts
38@Component({
39 selector: 'todos',
40 template: `
41 @if (todos.isPending()) {
42 <span>Loading...</span>
43 } @else if (todos.isError()) {
44 <span>Error: {{ todos.error()?.message }}</span>
45 } @else {
46 <!-- We can assume by this point that status === 'success' -->
47 @for (todo of todos.data(); track todo.id) {
48 <li>{{ todo.title }}</li>
49 } @empty {
50 <li>No todos found</li>
51 }
52 }
53 `,
54})
55export class PostsComponent {
56 todos = injectQuery(() => ({
57 queryKey: ['todos'],
58 queryFn: fetchTodoList,
59 }))
60}
61```
62β
63[//]: # 'Example3'
64β
65If booleans aren't your thing, you can always use the `status` state as well:
66β
67[//]: # 'Example4'
68β
69```angular-ts
70@Component({
71 selector: 'todos',
72 template: `
73 @switch (todos.status()) {
74 @case ('pending') {
75 <span>Loading...</span>
76 }
77 @case ('error') {
78 <span>Error: {{ todos.error()?.message }}</span>
79 }
80 <!-- also status === 'success', but "else" logic works, too -->
81 @default {
82 <ul>
83 @for (todo of todos.data(); track todo.id) {
84 <li>{{ todo.title }}</li>
85 } @empty {
86 <li>No todos found</li>
87 }
88 </ul>
89 }
90 }
91 `,
92})
93class TodosComponent {}
94```
95β
96[//]: # 'Example4'
97[//]: # 'Materials'
98[//]: # 'Materials'