v-for用于遍历vue中的数据,可以遍历数组、对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<div id="app">
<p v-for="(value,key,index) in stu">{{index}} {{key}} {{value}}</p>
<hr>
<ul>
<li v-for="(item,index) in arr">{{index}} {{item}}</li>
</ul>
<hr>
<table>
<tr>
<th>数组下标</th>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>成绩</th>
</tr>
<tr v-for="(stu,index) in stuList" :key="stu.id">
<td>{{index}}</td>
<td>{{stu.id}}</td>
<td>{{stu.name}}</td>
<td>{{stu.age}}</td>
<td>{{stu.score}}</td>
</tr>
</table>
</div>
</body>
</html>
<script src="js/vue.js"></script>
<script>
new Vue({
el: "#app",
data: {
arr: ["红色", "绿色", "蓝色"],
stu: {id: 1, name: "张三", age: 18, score: 90.6},
stuList: [
{id: 1, name: "张三", age: 18, score: 90.6},
{id: 2, name: "李四", age: 20, score: 88.3},
{id: 3, name: "王五", age: 19, score: 98.1},
]
}
});
</script>
|