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

README.md

16 lines1.3 KB

1# TanStack Angular Query Basic Example
2​
3A list of posts and a post detail view. Posts you have already opened are marked as visited from the query cache.
4​
5## How it works
6​
7- `src/app/services/posts/api.ts` is an `@Injectable` `PostsAPI` that returns `HttpClient` observables for the post list and a single post.
8- `src/app/services/posts/queries.ts` is an `@Injectable` `PostsQueries` whose `list()` and `detail(id)` return `queryOptions` and turn the observables into promises with `lastValueFrom`. The detail request is stopped with `takeUntil` when the query's `signal` aborts.
9- `src/app/components/posts.component.ts` reads the list with `injectQuery(() => this.postsQueries.list())`, and `src/app/components/posts.component.html` shows a post in bold green when `queryClient.getQueryData(postsQueries.detail(post.id).queryKey)` returns data.
10- `src/app/components/post.component.ts` passes the `postId` input signal to `injectQuery(() => this.#postsQueries.detail(this.postId()))`, so the query follows the selected post and shows "Background Updating..." while `isFetching()` is `true`.
11- `src/app/app.config.ts` registers the `QueryClient` with `provideTanStackQuery` and a default `gcTime` of 24 hours, plus `withDevtools()`.
12​
13To run this example:
14​
15- `npm install`
16- `npm start`