Data fetching in Vue usually runs inside onMounted, a watcher, or a composable. In this playground we simulate an API with setTimeout so you learn the state machine without CORS or flaky networks.
Typical flow
- Set
loadingto true when a request starts - Store results in a
refwhen data arrives - Capture failures in an
errorref or show fallback UI - Ignore stale responses if the user changes filters before the prior fetch finishes
Production note
Real apps use fetch, axios, or libraries like TanStack Query / Pinia actions. The loading → data → error pattern is the same whether data comes from REST, GraphQL, or server components in a meta-framework.
Self-check
- Why fetch in
onMountedinstead of top-levelsetupbody? - What happens if you forget to clear
loadingon error?
Pitfall: Fetching in bare setup without lifecycle can run twice in SSR—use onMounted in client-only playgrounds and apps.
Interview prep
- Where should fetch run in Composition API?
Typically
onMountedor a composable invoked from setup—set loading/error refs and guard against stale responses on unmount.