Introduction
In this project, you will learn how to dynamically display the homepage data using Vue.js. This project is designed to help you understand the process of fetching data from a JSON file and rendering it in a Vue.js component.
👀 Preview

🎯 Tasks
In this project, you will learn:
- How to request the homepage post list data via Axios and bind it to the
List.vuecomponent. - How to implement the layout style and effect of the homepage to match the provided design.
🏆 Achievements
After completing this project, you will be able to:
- Use Axios to fetch data from a JSON file.
- Bind the fetched data to a Vue.js component and render it in the template.
- Style the components to match a specific design.
- Integrate different Vue.js components to create a complete homepage.
Set Up the Project Structure
In this step, you will set up the project files and structure. Follow the steps below to complete this step:
Open the project folder. The directory structure is as follows:
├── public
│ ├── static
│ ├── data
│ ├── list.json
│ ├── images
│ ├── favicon.ico
│ ├── index.html
├── src
│ ├── assets
│ ├── components
│ ├── Header.vue
│ ├── List.vue
│ ├── Recommend.vue
│ ├── router
│ ├── views
│ ├── Home.vue
│ ├── App.vue
│ ├── main.js
└── package-lock.json
└── package.json
└── README.md
Among them:
components/List.vueis the article list component to be added for this challenge.public/static/data/list.jsonis the data file to get the list of articles.public/static/imagesis the image file used for the list of articles.
Next, download the dependencies using the npm install command in the terminal, wait for the dependencies to finish downloading and then start the project using the npm run serve command.
Once the project starts successfully, click on the "Web 8080" to preview it in your browser. The running effect of the page is shown in the following image.

Completion of the List Display Function
In this step, you will learn how to request the homepage post list data via axios and bind it to the List.vue component.
- Open the
List.vuefile located in thesrc/componentsfolder. - Import the
axioslibrary under<script>:
// TODO
import axios from "axios";
- In the
data()function, define adataListproperty to store the list data:
data() {
return {
dataList: []
};
}
- In the
created()lifecycle hook, call thegetList()method to fetch the list data:
created() {
this.getList();
}
- Implement the
getList()method to fetch the data from thepublic/static/data/list.jsonfile:
methods: {
getList() {
axios.get("./static/data/list.json").then((res) => {
this.dataList = res.data.data.listInfo;
});
}
}
- The complete code within the
<script>is as follows:
<script>
// TODO
import axios from "axios";
export default {
data() {
return {
dataList: []
};
},
created() {
this.getList();
},
methods: {
getList() {
axios.get("./static/data/list.json").then((res) => {
this.dataList = res.data.data.listInfo;
});
}
}
};
</script>
- In the template, use a
v-fordirective to render the list items:
<template>
<div class="list">
<!-- TODO -->
<div class="list-item" v-for="item in dataList" :key="item.id">
<img class="list-pic" :src="item.imgUrl" />
<div class="list-info">
<p class="title">{{ item.title }}</p>
<p class="desc">{{ item.desc }}</p>
</div>
</div>
</div>
</template>
This step ensures that the homepage post list data is fetched from the public/static/data/list.json file and bound to the List.vue component.
By following these steps, you have completed the dynamization of the homepage data, including fetching the data from the public/static/data/list.json file and rendering the list items in the desired layout.
The completed effect is as follows:

Summary
Congratulations! You have completed this project. You can practice more labs in LabEx to improve your skills.



