Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

example-query.component.ts

46 lines1.2 KB

1import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
2import { injectQuery } from '@tanstack/angular-query-experimental'
3import { HttpClient } from '@angular/common/http'
4import { lastValueFrom } from 'rxjs'
5​
6interface Response {
7 name: string
8 description: string
9 subscribers_count: number
10 stargazers_count: number
11 forks_count: number
13​
14@Component({
15 changeDetection: ChangeDetectionStrategy.OnPush,
16 selector: 'example-query',
17 standalone: true,
18 template: `
19 <div style="padding-bottom: 20px">
20 @if (query.isPending()) {
21 <div>Loading...</div>
22 }
23 @if (query.isError()) {
24 <div>An error has occurred: {{ query.error().message }}</div>
25 }
26 @if (query.data(); as data) {
27 <h1>{{ data.name }}</h1>
28 <p>{{ data.description }}</p>
29 <strong>πŸ‘€ {{ data.subscribers_count }}</strong>
30 <strong>✨ {{ data.stargazers_count }}</strong>
31 <strong>🍴 {{ data.forks_count }}</strong>
32 }
33 </div>
34 `,
35})
36export class ExampleQueryComponent {
37 #http = inject(HttpClient)
38​
39 query = injectQuery(() => ({
40 queryKey: ['repoData'],
41 queryFn: () =>
42 lastValueFrom(
43 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
44 ),
45 }))