概述
使用v-for遍历对象数组在真实的开发中也属于非常常见的用法,需要重点掌握。
因为目前流行的是前后端分离开发,在前后端分离开发中,最常需要处理的就是对象数组类型的数据了。
比如,将员工信息渲染到表格中。
这节课我们就来使用v-for渲染对象数组的方式实现一下这个案例。
基本用法
我们创建src/components/Demo20.vue,在这个组件中,我们要:
- 1:定义employees,表示员工对象数组,其中每个元素有id、name、age、gender、salary字段。
- 2:创建一个表格
- 3:在表格中使用v-for遍历employees并渲染
- 4:稍微用CSS样式美化一下表格
代码如下:
<script setup>
const employees = [
{
id: 1,
name: "张三",
age: 23,
gender: "男",
salary: 30000,
},
{
id: 2,
name: "李四",
age: 23,
gender: "男",
salary: 28000,
},
{
id: 3,
name: "王五",
age: 23,
gender: "男",
salary: 18000,
},
]
</script>
<template>
<div>
<table>
<thead>
<tr>
<th>#</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>薪资</th>
</tr>
</thead>
<tbody>
<tr v-for="(employee, index) in employees" :key="employee.id">
<td>{{ index + 1 }}</td>
<td>{{ employee.name }}</td>
<td>{{ employee.gender }}</td>
<td>{{ employee.age }}</td>
<td>{{ employee.salary }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<style>
table {
width: 500px;
text-align: center;
border-collapse: collapse;
}
tr, td, th {
border: 1px solid #ccc;
}
th, td {
padding: 5px;
}
</style>
接着,我们修改src/App.vue,引入Demo20.vue并进行渲染:
<script setup>
import Demo from "./components/Demo20.vue"
</script>
<template>
<h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
<hr>
<Demo/>
</template>
然后,我们浏览器访问:http://localhost:5173/
完整代码
package.json
{
"name": "hello",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build"
},
"dependencies": {
"vue": "^3.3.8"
},
"devDependencies": {
"@vitejs/plugin-vue": "^4.5.0",
"vite": "^5.0.0"
}
}
vite.config.js
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
export default defineConfig({
plugins: [vue()],
})
index.html
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + Vue</title>
</head>
<body>
<div id="app"></div>
<script type="module" src="/src/main.js"></script>
</body>
</html>
src/main.js
import { createApp } from 'vue'
import App from './App.vue'
createApp(App).mount('#app')
src/App.vue
<script setup>
import Demo from "./components/Demo20.vue"
</script>
<template>
<h1>欢迎跟着Python私教一起学习Vue3入门课程</h1>
<hr>
<Demo/>
</template>
src/components/Demo20.vue
<script setup>
const employees = [
{
id: 1,
name: "张三",
age: 23,
gender: "男",
salary: 30000,
},
{
id: 2,
name: "李四",
age: 23,
gender: "男",
salary: 28000,
},
{
id: 3,
name: "王五",
age: 23,
gender: "男",
salary: 18000,
},
]
</script>
<template>
<div>
<table>
<thead>
<tr>
<th>#</th>
<th>姓名</th>
<th>性别</th>
<th>年龄</th>
<th>薪资</th>
</tr>
</thead>
<tbody>
<tr v-for="(employee, index) in employees" :key="employee.id">
<td>{{ index + 1 }}</td>
<td>{{ employee.name }}</td>
<td>{{ employee.gender }}</td>
<td>{{ employee.age }}</td>
<td>{{ employee.salary }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<style>
table {
width: 500px;
text-align: center;
border-collapse: collapse;
}
tr, td, th {
border: 1px solid #ccc;
}
th, td {
padding: 5px;
}
</style>
启动方式
yarn
yarn dev
浏览器访问:http://localhost:5173/