[javascript] view plain copy
Documentcolor: blue;
}
- {{$index}}.{{ item.msg }}vm.items.splice(index, 1)
vm.remove
vm.items[0] = {} 失效
vm.items.$set(0, { childMsg: 'Changed!'})
vm.items.length = 0 失效
vm.items={}
因为 JavaScript 的限制,Vue.js 不能检测到下面数组变化:
直接用索引设置元素,如 vm.items[0] ={};
修改数据的长度,如 vm.items.length= 0。
var vm = newVue({
el:'#example-1',
data: {
items: [{
msg:'Foo'}, {
msg:'Bar'}, {
msg:'George'}]
},
methods: {
f1:function() {
vm.items[0] = {}; //失效
},
f2:function() {
vm.items.$set(0, {
childMsg:'Changed!'})
vm.items.$set(2, {
msg:'dongtao!'})
},
f3:function() {
vm.items.length= 0; //失效
},
f4:function() {
vm.items={}
},
f5:function(item) {var index = this.items.indexOf(item) //Search an array for the item
if (index !== -1) {this.items.splice(index, 1) //Selects a part of an array, and returns the new array
}
},
f6:function(item) {this.items.$remove(item)
}
}
})