examples/angular/basic-persister
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Angular Query Basic Persister Example#
A 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.
How it works#
src/app/services/posts/queries.tsis an@InjectablePostsQuerieswhoselist()anddetail(id)returnqueryOptionsand turn theHttpClientobservables fromsrc/app/services/posts/api.tsinto promises withlastValueFrom.src/app/components/posts.component.tsreads the list withinjectQuery(() => this.postsQueries.list()), andsrc/app/components/posts.component.htmlshows a post in bold green whenqueryClient.getQueryData(postsQueries.detail(post.id).queryKey)returns data.src/app/components/post.component.tspasses thepostIdinput signal toinjectQuery(() => this.#postsQueries.detail(this.postId())), so the query follows the selected post.src/app/app.config.tsaddswithPersistQueryClienttoprovideTanStackQuerywith alocalStoragepersister fromcreateAsyncStoragePersister. TheQueryClientsets a defaultstaleTimeof 1 minute and agcTimeof 24 hours, and the persisted cache is restored when the page reloads.
To run this example:
npm installnpm start