数组经常和v-for结合使用
语法是(item,index)in 数据
item和index 可以结合其他指令一起使用
数组长度的更新会同步到页面上,是响应式的
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>v-for指令</title>
</head>
<body>
<div id="app">
<input type="button" value="添加数据" @click="add" />
<input type="button" value="移除数据" @click="remove" />
<ul>
<li v-for="(it,index) in arr" :title="it">
{{ index+1 +"."}}发达地区:{{it}}
</li>
<p2 v-for="(item,index) in objArr" v-bind:title="item.name">
{{ item.name }}
</p2>
</ul>
</div>
<!-- 开发环境版本,包含了有帮助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<script>
var app = new Vue({
el:"#app",
data:{
arr:["北京","上海","广州","深圳"],
objArr:[
{name:"zhangyue"},
{name:"zhanglou"}
]
},
methods:{
add:function(){
this.objArr.push({name:"huihui"});
},
remove:function(){
this.objArr.shift();
}
}
})
</script>
</body>
</html>