1、代码如下:
列表渲染
测试: v-for 遍历数组
{{index}}--{{p.name}}--{{p.age}}
--删除
--更新
添加
测试: v-for 遍历对象
{{key}}={{item}}
new Vue({
el: '#demo',
data: {
persons: [
{name: 'Tom', age:18},
{name: 'Jack', age:17},
{name: 'Bob', age:19},
{name: 'Mary', age:16}
]
},
methods: {
deleteP (index) {
this.persons.splice(index, 1) // 调用了不是原生数组的splice(), 而是一个变异(重写)方法
// 1. 调用原生的数组的对应方法
// 2. 更新界面
},
updateP (index, newP) {
console.log('updateP', index, newP)
// this.persons[index] = newP // vue根本就不知道
this.persons.splice(index, 1, newP)
// this.persons = []
},
addP (newP) {
this.persons.push(newP)
}
}
})
2、效果图如下: 1)初始页面效果图: 2)点击删除按钮效果图:
3)点击更新按钮效果图:
4)点击添加按钮效果图: