Consider this Vue 3 component using the Composition API. What will the user see while the data is being fetched?
<script setup> import { ref, onMounted } from 'vue' const loading = ref(true) const data = ref(null) onMounted(() => { setTimeout(() => { data.value = 'Hello Vue!' loading.value = false }, 2000) }) </script> <template> <div> <p v-if="loading">Loading data...</p> <p v-else>{{ data }}</p> </div> </template>
Look at the loading variable and how it controls the displayed text.
The loading ref starts as true, so the template shows 'Loading data...'. After 2 seconds, loading becomes false and data is set, so the message changes to 'Hello Vue!'.
In this Vue 3 component, what will be the value of isLoading after fetchData() finishes?
<script setup> import { ref } from 'vue' const isLoading = ref(false) const data = ref(null) async function fetchData() { isLoading.value = true data.value = await new Promise(resolve => setTimeout(() => resolve('Done'), 1000)) isLoading.value = false } fetchData() </script>
Check when isLoading is set to false in the async function.
The isLoading ref is set to true before awaiting the promise, then set to false after the promise resolves. So after fetchData() finishes, isLoading is false.
Choose the code snippet that correctly manages a loading state while fetching data asynchronously.
Remember how to correctly update reactive state in Vue 3.
Option B uses reactive and updates state.loading correctly inside a promise. Option B is missing await on fetch and does not handle the promise properly. Options C and D try to assign directly to refs without .value, causing errors.
Examine the code below. The loading spinner shows briefly but disappears too soon. What is the cause?
<script setup> import { ref, onMounted } from 'vue' const loading = ref(true) onMounted(() => { fetch('/api/data') .then(response => response.json()) .then(data => { // process data }) loading.value = false }) </script> <template> <div> <div v-if="loading">Loading spinner...</div> <div v-else>Data loaded</div> </div> </template>
Check when loading.value is updated relative to the fetch call.
The code sets loading.value = false immediately after starting fetch, not waiting for the data to load. This causes the spinner to disappear too soon.
When showing a loading spinner in a Vue component, which practice best improves accessibility for screen reader users?
Think about how screen readers detect loading and how to inform users.
Using aria-busy="true" signals to assistive technologies that content is loading. Adding visually hidden text helps users understand the loading state. Options B, C, and D either hide important info or block interaction without feedback, which harms accessibility.