Complete the code to import the function used for HTTP requests in Vue.
import { [1] } from '@vueuse/core';
The useFetch function is a common Vue composable for making HTTP requests reactively.
Complete the code to catch errors from the HTTP call using try and {{BLANK_1}}.
try { const response = await fetch(url); // process response } catch ([1]) { console.error(error); }
The catch block uses a variable to hold the error object, commonly named error.
Fix the error in the code to check if the HTTP response was successful using {{BLANK_1}}.
const response = await fetch(url); if (!response.[1]) { throw new Error('Network response was not ok'); }
response.status directly without checking the range.success or valid.The response.ok property is a boolean indicating if the HTTP status is in the successful range.
Fill both blanks to handle errors in a Vue component using try and catch.
async function fetchData() {
try {
const data = await fetch(url);
// process data
} [1] ([2]) {
console.error(error);
}
}finally instead of catch to handle errors.The try block is paired with catch to handle errors, and the error variable is commonly named error.
Fill all three blanks to create a reactive error state in Vue and update it when an HTTP call fails.
<script setup> import { ref } from 'vue'; const error = [1](null); async function loadData() { try { const response = await fetch(url); if (!response.ok) throw new Error('Failed'); } catch ([2]) { error.value = [3].message; } } </script>
We use ref to create a reactive variable. The catch block uses err as the error variable, and we assign err.message to error.value.