examples/vue/suspense
| Name | Last commit message | Last commit date |
|---|---|---|
| .. | ||
README.md
TanStack Vue Query Suspense Example#
Loads projects in async components that await the query's suspense() inside <Suspense>.
How it works#
src/services/projects/queries.tsdefinesprojectsQueries.list()andprojectsQueries.detail(id),queryOptionsfactories for TanStack repositories on the GitHub API.src/services/projects/api.tsadds a 1-second delay to each request.src/components/ProjectList.vueandsrc/components/ProjectDetail.vuecalluseQuerywiththrowOnError: trueand thenawait suspense()in<script setup>, which makes them async components that suspend until the data arrives.src/App.vueloads both withdefineAsyncComponentinside<Suspense>, shows the#fallbackslot while they suspend, and catches query errors withonErrorCaptured.- Fetches start before the component suspends:
src/App.vuecallsqueryClient.query(projectsQueries.list())when showing the projects, andsrc/components/ProjectList.vuecallsqueryClient.query(projectsQueries.detail(project))when a project is clicked. src/transition.tsprovides anisPendingref that<Suspense>sets from itspendingandresolveevents, andsrc/components/BaseButton.vueuses it to show a spinner on the clicked button.
To run this example:
npm installnpm run dev