Open History to see the last commit
1import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
2import { injectQuery } from '@tanstack/angular-query'
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
12}
13β
14@Component({
15 changeDetection: ChangeDetectionStrategy.OnPush,
16 selector: 'example-query',
17 template: `
18 <div style="padding-bottom: 20px">
19 @if (query.isPending()) {
20 <div>Loading...</div>
21 }
22 @if (query.isError()) {
23 <div>An error has occurred: {{ query.error().message }}</div>
24 }
25 @if (query.isSuccess()) {
26 @let data = query.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 readonly #http = inject(HttpClient)
38β
39 readonly query = injectQuery(() => ({
40 queryKey: ['repoData'],
41 queryFn: () =>
42 lastValueFrom(
43 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
44 ),
45 }))
46}