vue 添加项id自动获取id

  • Post author:
  • Post category:vue


(function (window) {
‘use strict’;
const vm = new Vue ({
el:’#app’,
data:{
name:”,
list:[
{id:1,name:’抽烟’,completed:false},
{id:2,name:’喝酒’,completed:true},
{id:3,name:’烫头’,completed:false},
]
},
methods:{
add(){
if(this.name.trim() === ”){
return
}
let id

if(this.name.length === 0){
id = 1
}else{
id=this.name[this.name.length – 1].id+1
}

//数据为0的时候 -1产生的错误

this.list.push({id,name:this.name,completed:false})

this.name=”

}
},
})

})(window);

转载于:https://www.cnblogs.com/xiannv/p/10996603.html