In this episode, we're diving into how to fetch data in a Nuxt 3 app. We start by introducing the concept of data fetching in web apps—like making requests to external APIs to get the information that powers your apps. We'll use JSON Placeholder, a handy fake API, to grab a list of to-dos for our demo.
First, we walk through using useAsyncData
to fetch the to-dos. You'll see how to set it up with a key, make the API call using fetch
inside a closure, and return the data you need. We explain how this takes advantage of Vue 3's suspense feature, which can block page loading until the data comes back, giving users a smoother experience. Once the data is fetched, we destructure it to get just the info we want—like an array of to-do objects—and render them in a list. We even add NuxtLinks for each to-do so you can click through and see more details about each one.
There's also a quick look at an alternative: using useFetch
, which lets you make a request with even less setup (no need to manually define a key or callback). We show you how both methods work, and the page displays your list of to-dos either way.
Throughout, you'll pick up the basics of displaying remote data in your UI, and set yourself up for deeper dives into customizing data fetching (which we'll cover soon!).