v-for遍历
demo
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<div id="app">
<ul>
<li v-for="(it,index) in arr">{{index+1}}:{{it}}</li>
</ul>
<h2 v-for="item in vegetables" :title="item.name">{{item.name}}</h2>
<input type="button" value="添加" @click="add()" />
<input type="button" value="删除" @click="remove()" />
</div>
<body>
<script>
var app =new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
vegetables:[
{name:"蛋炒饭"},
{name:"芹菜炒牛肉"}
]
},
methods:{
add:function(){
this.vegetables.push({name:"牛肉粉"});
},
remove:function(){
this.vegetables.shift();
}
}
})
</script>
</body>
</html>
总结:v-for后面一般接 (item in 数组名)有时候需要用到索引就用(item,index in 数组名),其中item可以自定义