📖 Scenario: You are building a simple Vue app that shows a list of books fetched from an online API. This helps users see book titles without manually typing them.
🎯 Goal: Create a Vue component that fetches book data from an API and displays the titles in a list. This shows how API integration brings live data into your app.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive variable to hold book data
Fetch data from a sample API endpoint
Display the book titles in an unordered list
Handle loading state with a message
💡 Why This Matters
🌍 Real World
Many apps need to show live data from servers or services. API integration lets your app get fresh data automatically.
💼 Career
Understanding API integration is essential for frontend developers to build interactive and dynamic web applications.
Progress0 / 4 steps
1
Set up the initial data structure
Create a reactive variable called books initialized as an empty array using Vue's ref.
Vue
Hint
Use import { ref } from 'vue' and then const books = ref([]).
2
Add a loading state variable
Create a reactive variable called loading and set it to true to track if data is loading.
Vue
Hint
Use const loading = ref(true) to track loading state.
3
Fetch book data from the API
Use Vue's onMounted lifecycle hook to fetch data from 'https://api.example.com/books'. When data arrives, set books.value to the fetched array and set loading.value to false.
Vue
Hint
Use onMounted with an async function to fetch and update books and loading.
4
Display the book titles and loading message
In the template, show Loading... if loading is true. Otherwise, display an unordered list with each book's title inside a <li> using v-for with book and index as variables.
Vue
Hint
Use v-if and v-else to toggle loading message and list. Use v-for with book and index to list titles.
Practice
(1/5)
1. Why is API integration important in a Vue application?
easy
A. It allows the app to get and show live data from outside sources.
B. It makes the app run faster by avoiding any data fetching.
C. It removes the need for user interaction in the app.
D. It automatically styles the app without CSS.
Solution
Step 1: Understand API integration role
API integration connects the app to external data or services, allowing dynamic content.
Step 2: Identify the benefit in Vue apps
This connection lets the app show live or updated data, improving usefulness.
Final Answer:
It allows the app to get and show live data from outside sources. -> Option A
Quick Check:
API integration = live data connection [OK]
Hint: API integration means connecting to live data sources [OK]
Common Mistakes:
Thinking API integration speeds up app without data fetching
Believing it removes user interaction
Confusing API integration with styling
2. Which Vue code snippet correctly fetches data from an API inside onMounted?
easy
A. onMounted(async () => { const res = await fetch('/api/data'); data.value = await res.json(); })
B. onMounted(() => { fetch('/api/data').then(res => data = res.json()) })
C. onMounted(() => { const res = fetch('/api/data'); data.value = res.json(); })
D. onMounted(async () => { data = fetch('/api/data').json() })
Solution
Step 1: Check async fetch usage
onMounted(async () => { const res = await fetch('/api/data'); data.value = await res.json(); }) uses async/await correctly to wait for fetch and JSON parsing.
Step 2: Identify reactive assignment
It assigns the parsed JSON to data.value, which is correct for Vue refs.
Final Answer:
onMounted(async () => { const res = await fetch('/api/data'); data.value = await res.json(); }) -> Option A