咱们用 v-for 指令根据一组数组的选项列表进行渲染。 v-for 指令须要以 item in items 形式的特殊语法, items 是源数据数组而且 item 是数组元素迭代的别名。数组
<div v-for="item in items">{{item.text}}{{wenben}}</div>
data:{ items:[ {text:"chifan",isOk:true}, {text:"shuijue",isOk:false}, {text:"kandianshi",isOk:true}, {text:"dayouxi",isOk:true}, {text:"kandianying",isOk:false}, ] }
在 v-for 块中,咱们拥有对父做用域属性的彻底访问权限。 v-for 还支持一个可选的第二个参数为当前项的索引。app
<div v-for="(item,index) in items">{{index}}{{item.text}}{{wenben}}</div>
<div v-for="item of items"></div>
<div id="app"> <div v-for="value in xx"> {{value}}</div> </div> <script> var vm=new Vue({ el:"#app", data:{ xx:{ firstName: 'John', lastName: 'Doe', age: 30 } } }); </script>
你也能够提供第二个的参数为键名:ide
<div id="app"> <div v-for="(value,key) in xx">{{key}} is {{value}}</div> </div> <script> var vm=new Vue({ el:"#app", data:{ xx:{ firstName: 'John', lastName: 'Doe', age: 30 } } }); </script>
第三个参数为索引:ui
<div id="app"> <div v-for="(value,key,index) in xx">{{index}}{{key}} is {{value}}</div> </div> <script> var vm=new Vue({ el:"#app", data:{ xx:{ firstName: 'John', lastName: 'Doe', age: 30 } } }); </script>
自定义组件里,你能够像任何普通元素同样用 v-for 。this
2.2.0+ 的版本里,当在组件中使用 v-for 时,key 如今是必须的。spa
<div id="app"> <todo-item v-for="(item,index) in items" v-bind:tiaomu="item.text" :key="index"></todo-item> </div> <script> Vue.component("todo-item",{ template:`<div>{{this.tiaomu}}<input type="button" value="x"/></div>`, props:['tiaomu'] }) var vm=new Vue({ el:"#app", data:{ items:[ {text:"chifan",isOk:true}, {text:"shuijue",isOk:false}, {text:"kandianshi",isOk:true}, {text:"dayouxi",isOk:true}, {text:"kandianying",isOk:false}, ] } }); </script>
v-for 也能够取整数。在这种状况下,它将重复屡次模板。prototype
<span v-for="n in 10">{{ n }} </span>
<ul> <template v-for="item in items"> <li>{{item.msg}}</li> <li class="divider"></li> </template> </ul>
咱们想要显示一个数组的过滤或排序副本,而不实际改变或重置原始数据。在这种状况下,能够建立返回过滤或排序数组的计算属性。code
<div v-for="num in evenNum">{{num}}</div> <script> var vm=new Vue({ data:{ numbers:[1,2,3,4,5] }, computed:{ evenNum:function(){ return this.numbers.filter(function(item){ return item%2==0; }); } }, }); </script>
在计算属性不适用的状况下 (例如,在嵌套 v-for 循环中) 你能够使用一个 method 方法:component
<div v-for="num in even()">{{num}}</div> <script> var vm=new Vue({ data:{ numbers:[1,2,3,4,5] }, methods:{ even:function(){ return this.numbers.filter(function(item){ return item%2==0; }); } } }); </script>
Vue 包含一组观察数组的变异方法,因此它们也将会触发视图更新。这些方法以下:对象
变异方法(mutation method),顾名思义,会改变被这些方法调用的原始数组。相比之下,也有非变异(non-mutating method)方法,例如: filter(), concat() 和 slice() 。这些不会改变原始数组,但老是返回一个新数组。当使用非变异方法时,能够用新数组替换旧数组:
example1.items = example1.items.filter(function (item) { return item.message.match(/Foo/) })
1.当你利用索引直接设置一个项时,例如: vm.items[indexOfItem] = newValue
2.当你修改数组的长度时,例如: vm.items.length = newLength
为了解决第一类问题,如下两种方式均可以实现和 vm.items[indexOfItem] = newValue 相同的效果, 同时也将触发状态更新:
// Vue.set Vue.set(example1.items, indexOfItem, newValue)
// Array.prototype.splice example1.items.splice(indexOfItem, 1, newValue)
为了解决第二类问题,你能够使用 splice:
example1.items.splice(newLength)