📖 Scenario: You are building a simple Vue 3 app that shows a list of users fetched from a public API. This app will help you learn how to make GET requests inside Vue components using the Composition API.
🎯 Goal: Create a Vue component that fetches user data from https://jsonplaceholder.typicode.com/users when the component loads, and displays the users' names in a list.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive variable to store the users
Make a GET request to https://jsonplaceholder.typicode.com/users inside onMounted
Display the users' names in an unordered list
Handle the fetch asynchronously with async/await
💡 Why This Matters
🌍 Real World
Fetching data from APIs is a common task in web apps to show dynamic content like user lists, posts, or products.
💼 Career
Understanding how to make GET requests and display data in Vue components is essential for frontend developer roles working with modern web frameworks.
Progress0 / 4 steps
1
Setup the users data variable
Create a Vue 3 component with <script setup> and import ref from vue. Then create a reactive variable called users initialized to an empty array.
Vue
Hint
Use ref([]) to create a reactive array for users.
2
Add the API URL configuration
Add a constant called apiUrl and set it to the string 'https://jsonplaceholder.typicode.com/users' inside the <script setup>.
Vue
Hint
Use const apiUrl = 'https://jsonplaceholder.typicode.com/users'.
3
Fetch users data on component mount
Import onMounted from vue. Inside <script setup>, use onMounted to run an async function that fetches data from apiUrl, converts the response to JSON, and assigns it to users.value.
Vue
Hint
Use onMounted(async () => { ... }) and await fetch(apiUrl).
4
Display the users in the template
In the <template>, add an unordered list <ul>. Use v-for to loop over users and display each user's name inside a <li>. Add a unique :key binding with user.id.
Vue
Hint
Use <li v-for="user in users" :key="user.id">{{ user.name }}</li> inside a <ul>.
Practice
(1/5)
1. What is the main purpose of making a GET request inside a Vue component?
easy
A. To delete data on the server
B. To send data from the component to the server
C. To fetch data from a server to display in the component
D. To update the component's local state without server interaction
Solution
Step 1: Understand GET request purpose
A GET request is used to retrieve or fetch data from a server.
Step 2: Relate to Vue component usage
In Vue components, GET requests are used to get data to show in the UI.
Final Answer:
To fetch data from a server to display in the component -> Option C
Quick Check:
GET request = fetch data [OK]
Hint: GET requests retrieve data to show in components [OK]
Common Mistakes:
Confusing GET with POST (sending data)
Thinking GET updates local state only
Mixing GET with delete operations
2. Which of the following is the correct way to import and use the lifecycle hook to run code after a Vue component mounts for a GET request?
easy
A. import { mounted } from 'vue'; mounted(() => { /* fetch code */ });
B. import { onLoad } from 'vue'; onLoad(() => { /* fetch code */ });
C. import { useMounted } from 'vue'; useMounted(() => { /* fetch code */ });
D. import { onMounted } from 'vue'; onMounted(() => { /* fetch code */ });
Solution
Step 1: Identify correct lifecycle hook name
The correct Vue 3 lifecycle hook to run code after component mounts is onMounted.
Step 2: Confirm import and usage syntax
You import it from 'vue' and call it with a function: onMounted(() => { ... }).
Final Answer:
import { onMounted } from 'vue'; onMounted(() => { /* fetch code */ }); -> Option D
Quick Check:
Use onMounted for post-mount code [OK]
Hint: Use onMounted from 'vue' to run code after mount [OK]
Common Mistakes:
Using wrong hook names like mounted or onLoad
Forgetting to import onMounted
Calling lifecycle hooks incorrectly
3. Given this Vue <script setup> snippet, what will be logged to the console after the component mounts?
4. What is wrong with this Vue <script setup> code that tries to fetch data on mount?
import { onMounted } from 'vue';
onMounted(() => {
const response = fetch('https://api.example.com/data');
const data = response.json();
console.log(data);
});
medium
A. Missing async/await causing response and data to be Promises
B. Using onMounted instead of created hook
C. fetch URL is invalid syntax
D. console.log should be inside a separate function
Solution
Step 1: Identify async usage problem
fetch returns a Promise, so response is a Promise, not the actual response.
Step 2: Explain consequence of missing await
Calling response.json() on a Promise causes data to be a Promise too, so logging it shows a Promise object, not data.
Final Answer:
Missing async/await causing response and data to be Promises -> Option A
Quick Check:
Always await fetch and json() [OK]
Hint: Always use async/await with fetch to get real data [OK]
Common Mistakes:
Not marking onMounted callback async
Not awaiting fetch and json() calls
Expecting immediate data without Promises
5. You want to fetch user data from an API and show a loading message while waiting. Which Vue <script setup> code correctly handles loading, success, and error states for a GET request?
hard
A. import { ref, onMounted } from 'vue';
const user = ref(null);
const loading = ref(true);
const error = ref(null);
onMounted(async () => {
try {
const res = await fetch('https://api.example.com/user');
if (!res.ok) throw new Error('Fetch failed');
user.value = await res.json();
} catch (e) {
error.value = e.message;
} finally {
loading.value = false;
}
});