v-for可以遍历数组,对象数组,对象
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Document</title>
<script src="lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
v-for循环数组
<p v-for="(item, i) in list1">{{item}}------index:{{i}}</p>
v-for循环对象数组
<p v-for="usr in list2">index:{{ usr.id }}------name:{{ usr.name }}</p>
v-for循环对象
<p v-for="(val, key, index) in user">value:{{val}}-------key:{{key}}------index:{{index}}</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
list1:[1,2,3,4,5,6],
list2:[
{id:1, name:'a'},
{id:2, name:'b'},
{id:3, name:'c'},
{id:4, name:'d'},
],
user:{
id:1,
name:'Francis',
gender: 'male',
}
},
})
</script>
</body>
</html>
v-for迭代数字从一开始
<p v-for="count in 10">loop{{count}}</p>
需要使用一个v-bind绑定一个唯一的key值,使对象数组键值对应。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script src="lib/vue-2.4.0.js"></script>
</head>
<body>
<div id="app">
<label>ID:
<input type="text" v-model="id">
</label>
<label>Name
<input type="text" v-model="name">
</label>
<label>
<input type="button" value="Add" @click="add">
</label>
<!--注意v-for循环的时候,key属性只能使用number获取string-->
<!--注意key在使用的时候,必须使用v-bind属性绑定的形式,指定key的值-->
<!--在组件中,使用v-for循环的时候,或者在一些特殊情况中,如果v-for有问题,必须在使用v-for的同时,指定唯一的字符串/数字类型-->
<p v-for="item in list" :key="item.id">
<input type="checkbox">
{{item.id}}----------------{{item.name}}
</p>
</div>
<script>
var vm = new Vue({
el: '#app',
data: {
id:'',
name:'',
list: [
{id:1, name:'a'},
{id:2, name:'b'},
{id:3, name:'c'},
{id:4, name:'d'},
]
},
methods: {
add()
{
this.list.unshift({id:this.id, name:this.name})
}
},
})
</script>
</body>
</html>
<p v-for="item in items" :key="item.id"> </p>