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

simple-example.component.ts

48 lines1.3 KB

1import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
2import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
3import { injectQuery } from '@tanstack/angular-query-experimental'
4import { HttpClient } from '@angular/common/http'
5import { lastValueFrom } from 'rxjs'
6​
7type Response = {
8 name: string
9 description: string
10 subscribers_count: number
11 stargazers_count: number
12 forks_count: number
14​
15@Component({
16 changeDetection: ChangeDetectionStrategy.OnPush,
17 selector: 'simple-example',
18 standalone: true,
19 template: `
20 @if (query.isPending()) {
21 Loading...
22 }
23 @if (query.error()) {
24 An error has occurred: {{ query.error().message }}
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​
34 <angular-query-devtools initialIsOpen />
35 `,
36 imports: [AngularQueryDevtools],
37})
38export class SimpleExampleComponent {
39 http = inject(HttpClient)
40​
41 query = injectQuery(() => ({
42 queryKey: ['repoData'],
43 queryFn: () =>
44 lastValueFrom(
45 this.http.get<Response>('https://api.github.com/repos/tanstack/query'),
46 ),
47 }))