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

example-query.component.ts

45 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 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 readonly #http = inject(HttpClient)
37​
38 readonly query = injectQuery(() => ({
39 queryKey: ['repoData'],
40 queryFn: () =>
41 lastValueFrom(
42 this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),
43 ),
44 }))