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

README.md

15 lines1.2 KB

1# TanStack Angular Query Basic Persister Example
2​
3A list of posts and a post detail view. Posts you have already opened are marked as visited from the query cache. The cache is persisted to `localStorage`.
4​
5## How it works
6​
7- `src/app/services/posts/queries.ts` is an `@Injectable` `PostsQueries` whose `list()` and `detail(id)` return `queryOptions` and turn the `HttpClient` observables from `src/app/services/posts/api.ts` into promises with `lastValueFrom`.
8- `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.
9- `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.
10- `src/app/app.config.ts` adds `withPersistQueryClient` to `provideTanStackQuery` with a `localStorage` persister from `createAsyncStoragePersister`. The `QueryClient` sets a default `staleTime` of 1 minute and a `gcTime` of 24 hours, and the persisted cache is restored when the page reloads.
11​
12To run this example:
13​
14- `npm install`
15- `npm start`