examples/vue/contacts-vue-router
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue Query Contacts Example (Vue Router)#
A contacts app whose router loaders read from the query cache and whose mutations invalidate it.
Built with Vue Router.
How it works#
src/services/contacts/queries.tsdefinescontactsQueries.list(q)andcontactsQueries.detail(id)withqueryOptions. The detail query throwsNot Foundwhen the contact does not exist.src/router.tsrunsqueryClient.querywithstaleTime: 'static'for the list and, on contact routes, the detail inrouter.beforeResolve, so each view renders with data already in the cache.src/queryClient.tscreates that sharedQueryClientwith a 10-secondstaleTime, andsrc/main.tspasses it toVueQueryPlugin.src/routes/RootView.vuereads the list withuseQueryand theqsearch param, debounces typing withuseDebouncerfrom@tanstack/vue-pacerbefore updating the URL, and shows a searching state fromuseIsFetching.src/routes/NewView.vue,src/routes/EditView.vue,src/routes/ContactView.vueandsrc/FavoriteButton.vuecalluseMutationand invalidatecontactsQueries.lists()orcontactsQueries.all()on success.src/FavoriteButton.vueshows the pendingvariablesas the favorite state until the mutation finishes.src/services/contacts/api.tsstores contacts inlocalStorageand waits a random time up to 800 ms on each call to simulate the network.
To run this example:
npm installnpm run dev