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

post.component.ts

35 lines994 Bytes

1import {
2 ChangeDetectionStrategy,
3 Component,
4 inject,
5 input,
6 numberAttribute,
7} from '@angular/core'
8import { RouterLink } from '@angular/router'
9import { injectQuery } from '@tanstack/angular-query'
10import { lastValueFrom } from 'rxjs'
11import { PostsService } from '../services/posts-service'
12​
13@Component({
14 changeDetection: ChangeDetectionStrategy.OnPush,
15 selector: 'post',
16 templateUrl: './post.component.html',
17 imports: [RouterLink],
18})
19export default class PostComponent {
20 readonly #postsService = inject(PostsService)
21​
22 // The Angular router will automatically bind postId
23 // as `withComponentInputBinding` is added to `provideRouter`.
24 // See https://angular.dev/api/router/withComponentInputBinding
25 readonly postId = input.required({
26 transform: numberAttribute,
27 })
28​
29 readonly postQuery = injectQuery(() => ({
30 queryKey: ['post', this.postId()],
31 queryFn: () => {
32 return lastValueFrom(this.#postsService.postById$(this.postId()))
33 },
34 }))