v-for循环列表
其中item为内容,index为下标
注意这里的key为唯一值,尽量不使用index
<div id="app">
<div v-for="(item, index) of list" :key="item.id">
{{item.text}} ---- {{index}}
</div>
</div>
<script>
var app = new Vue({
el:"#app",
data:{
list: [
{
id:"0001",
text:"min"
},
{
id:"0002",
text:"box"
},
{
id:"0003",
text:"xiaode"
}]
},
})
</script>
效果
若想改变数组中的内容不能直接通过改变下标的方式,这样数组中的内容虽然改变了,但是页面中的内容不会跟着改变,这个时候应该使用vue提供的一些方法 push pop shift unshift splic sort reverse,或者通过改变list的引用,和set方法
使用template占位符进行多个循环这样外层的div块就不会显示出来
html
<div id="app">
<template v-for="(item, index) of list" :key="item.id">
<div >
{{item.text}} ---- {{index}}
</div>
<span>
{{item.text}}
</span>
</template>
</div>
v-for循环对象
其中item为值,key键,index为下标
<div id="app">
<div v-for="(item, key, index) of userInfo">
{{item}} --- {{key}} --- {{index}}
</div>
</div>
<script>
//push pop shift unshift splic sort reverse
var app = new Vue({
el:"#app",
data:{
userInfo:{
name:"Min",
age: 18,
gender: "boy",
}
},
})
</script>
若要通过命令的方式直接修改对象中的值,这样页面不会随之而改变,可以修改该对象的引用,或者使用vue的set方法