<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>v-for</title>
</head>
<body>
<div id="app">
<ul>
<li v-for="(num,idx) in numList">
索引:{{idx}} 值:{{num}}
</li>
</ul>
<hr>
<input type="button" value="add" @click="add">
<input type="button" value="remove" @click="remove">
<h2 v-for="obj in objArr">
{{obj.name}}
</h2>
</div>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el: '#app',
data: {
numList: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
objArr: [{
name: 'aaaaa'
},
{
name: 'bbbbb'
},
{
name: 'ccccc'
},
]
},
methods: {
add: function () {
this.objArr.push({
name: new Date().getTime()
})
},
remove: function () {
this.objArr.shift();
}
}
})
</script>
</body>
</html>