📖 Scenario: You are building a simple Vue app that fetches user data from an API. To improve user experience, you want to show a loading message while the data is being fetched.
🎯 Goal: Create a Vue component that shows a loading message while fetching data and then displays the user name once loaded.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive loading signal to track loading state
Create a reactive user object to hold fetched data
Simulate data fetching with setTimeout
Show Loading... text while loading is true
Show user name after loading is false
💡 Why This Matters
🌍 Real World
Loading states are common in web apps to inform users that data is being fetched or processed, preventing confusion or frustration.
💼 Career
Understanding loading states and reactive UI updates is essential for frontend developers building responsive and user-friendly applications.
Progress0 / 4 steps
1
Setup reactive user data
Create a Vue component with <script setup> and define a reactive variable called user initialized to an empty object {}.
Vue
Hint
Use reactive from Vue to create a reactive object called user.
2
Add loading state variable
Add a reactive boolean variable called loading initialized to true inside the <script setup> section.
Vue
Hint
Use ref from Vue to create a boolean loading variable set to true.
3
Simulate data fetching and update states
Inside <script setup>, use setTimeout to simulate fetching data after 2 seconds. Inside the timeout callback, set user.name to 'Alice' and set loading.value to false.
Vue
Hint
Use setTimeout to delay 2 seconds, then update user.name and loading.value.
4
Add template to show loading and user name
Add a <template> section that shows Loading... text when loading.value is true, and shows User: {{ user.name }} when loading.value is false.
Vue
Hint
Use v-if to show loading text and v-else to show user name.
Practice
(1/5)
1. What is the main purpose of using a loading state in a Vue component?
easy
A. To disable Vue reactivity
B. To show users that data is being fetched or processed
C. To permanently hide content from users
D. To increase the component's size
Solution
Step 1: Understand user experience during data fetching
Loading states inform users that the app is working on a task, preventing confusion.
Step 2: Identify the purpose of loading states in Vue
They show progress or waiting messages while data loads or actions complete.
Final Answer:
To show users that data is being fetched or processed -> Option B
Quick Check:
Loading states = user feedback [OK]
Hint: Loading states show progress to users during waits [OK]
Common Mistakes:
Thinking loading states hide content permanently
Confusing loading states with disabling reactivity
Assuming loading states change component size
2. Which Vue syntax correctly shows a loading message only when isLoading is true?
easy
A. <div v-show="isLoading">Loading...</div>
B. <div v-if="!isLoading">Loading...</div>
C. <div v-if="isLoading">Loading...</div>
D. <div v-bind:isLoading>Loading...</div>
Solution
Step 1: Understand v-if usage
v-if conditionally renders the element only if the expression is true.
Step 2: Match condition to show loading
We want to show the message when isLoading is true, so v-if="isLoading" is correct.
Final Answer:
<div v-if="isLoading">Loading...</div> -> Option C
Quick Check:
Show loading if isLoading true = v-if="isLoading" [OK]
Hint: Use v-if with the loading variable directly [OK]
Common Mistakes:
Using v-if with negation (!isLoading) to show loading
Using v-show which toggles visibility but keeps element
Using invalid v-bind syntax for conditional rendering
Directly assigning to ref variable instead of its .value -> Option D
Quick Check:
Update ref with .value, not direct assignment [OK]
Hint: Change ref values with .value property [OK]
Common Mistakes:
Assigning ref variable directly instead of .value
Forgetting to handle promise with then or async/await
Assuming fetch syntax is wrong when it is correct
5. You want to disable a submit button during loading in Vue. Which code snippet correctly disables the button and shows loading text using isLoading reactive variable?
hard
A. <button :disabled="isLoading">{{ isLoading ? 'Loading...' : 'Submit' }}</button>
B. <button v-if="isLoading" disabled>Submit</button><button v-else>Loading...</button>
C. <button :disabled="!isLoading">Submit</button>
D. <button :loading="isLoading">Submit</button>
Solution
Step 1: Disable button based on loading state
Use :disabled="isLoading" to disable the button when loading.
Step 2: Show dynamic button text
Use interpolation with a ternary to show "Loading..." or "Submit" based on isLoading.