Complete the code to fetch data using useFetch in a Nuxt component.
<script setup>
const { data, error } = [1]('https://api.example.com/items')
</script>useAsyncData instead of useFetch when the question asks specifically for useFetch.fetchData.The useFetch composable is used in Nuxt to fetch data reactively in components.
Complete the code to fetch data using useAsyncData with a key in Nuxt.
<script setup>
const { data, error } = [1]('users', () => $fetch('/api/users'))
</script>useFetch without a key when the question requires useAsyncData.fetchAsync.useAsyncData is used with a key and a fetcher function to load data asynchronously in Nuxt.
Fix the error in the code by completing the blank with the correct property to access the fetched data.
<script setup>
const { data } = useFetch('/api/posts')
const posts = data[1]
</script>data.data which does not exist.data() as if it were a function.In Vue 3 and Nuxt, useFetch returns a ref, so you access the actual data with .value.
Fill both blanks to create a reactive data fetch with useAsyncData and handle errors.
<script setup>
const { data, error } = [1]('products', () => $fetch('/api/products'))
if (error[2]) {
console.error('Failed to load products')
}
</script>useFetch when a key is required.useAsyncData fetches data with a key, and checking error !== null detects if an error occurred.
Fill all three blanks to create a reactive fetch with useFetch, destructure data, and check loading state.
<script setup>
const { data, error, [1] } = [2]('https://api.example.com/info')
if (![3]) {
console.log('Loading data...')
}
</script>loading property which does not exist in useFetch.useAsyncData without a key.useFetch returns a pending property to indicate loading state. We destructure it and check if loading is ongoing.