Complete the code to import the function needed to fetch data in Vue 3 Composition API.
<script setup> import { [1] } from 'vue' </script>
In Vue 3 Composition API, ref is used to create reactive data, which is essential for API data handling.
Complete the code to fetch data from an API using fetch inside Vue's onMounted lifecycle hook.
<script setup> import { ref, onMounted } from 'vue' const data = ref(null) onMounted(() => { fetch('[1]') .then(res => res.json()) .then(json => data.value = json) }) </script>
Using a full URL like 'https://api.example.com/data' is common for API calls to fetch real data.
Fix the error in the code to correctly update the reactive data after fetching API response.
<script setup> import { ref } from 'vue' const info = ref(null) fetch('https://api.example.com/info') .then(response => response.json()) .then(data => [1] = data) </script>
In Vue 3, to update a ref's value, you must assign to refVariable.value.
Fill both blanks to create a reactive variable and fetch data on component mount.
<script setup> import { [1], onMounted } from 'vue' const userData = [2](null) onMounted(() => { fetch('https://api.example.com/user') .then(res => res.json()) .then(json => userData.value = json) }) </script>
Use ref to create a reactive variable holding simple data like null.
Fill all three blanks to create a reactive variable, fetch API data, and handle errors.
<script setup> import { [1], onMounted } from 'vue' const posts = [2]([]) const error = [3](null) onMounted(async () => { try { const res = await fetch('https://api.example.com/posts') posts.value = await res.json() } catch (e) { error.value = e.message } }) </script>
Use ref to create reactive variables for arrays and error messages.