Open History to see the last commit
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
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.data(); as data) {
26 <h1>{{ data.name }}</h1>
27 <p>{{ data.description }}</p>
28 <strong>π {{ data.subscribers_count }}</strong>
29 <strong>β¨ {{ data.stargazers_count }}</strong>
30 <strong>π΄ {{ data.forks_count }}</strong>
31 }
32 </div>
33 `,
34})
35export class ExampleQueryComponent {
36 #http = inject(HttpClient)
37β
38 query = injectQuery(() => ({
39 queryKey: ['repoData'],
40 queryFn: () =>
41 lastValueFrom(
42 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
43 ),
44 }))
45}