In Vue Basics, Part 6, we learned to manage state shared by several components centrally using Pinia.
Our app can now organize its internal data flow cleanly.
In Part 7, we finally move beyond the Vue application itself and communicate with a server.
Most modern web apps do not keep all their data locally; instead, they request it from a server when needed and update the interface dynamically.
This communication is called
API integration
.
It runs asynchronously, and today we will focus on axios, a library that makes asynchronous communication convenient to handle.
First, install Axios in the project.
Run the following command in the terminal.
npm install axios
After installation, create an Axios instance with common configuration, such as the API base URL, as you would in a production project.
Create an api folder under src, then add index.js.
src/api/index.js
import axios from 'axios';// Axios ์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค.const api = axios.create({ // API ์์ฒญ์ ๊ธฐ๋ณธ ์ฃผ์๋ฅผ ์ค์ ํฉ๋๋ค. // ๋ชจ๋ ์์ฒญ ์์ ์ด ์ฃผ์๊ฐ ๋ถ๊ฒ ๋ฉ๋๋ค. baseURL: 'https://jsonplaceholder.typicode.com'});export default api;
With this instance in place, write concise calls such as api.get('/posts') instead of axios.get('https://jsonplaceholder.typicode.com/posts').
In a real service, the network may be slow or the server may encounter a problem.
Without handling these situations, users may see only an empty screen and not understand why the app is not working.
Managing
loading
and
error
states is therefore important when calling APIs.
Use try...catch...finally to implement the logic cleanly.
We have learned how to integrate APIs so a Vue application can communicate with external servers.
Handling asynchronous data well is one of the essential skills for building dynamic modern web applications.
In Vue Basics, Part 8, we will learn to manage component styles and use conditional rendering to show or hide parts of the interface.