vue 添加项id自动获取id

孤者浪人 提交于 2020-03-13 17:24:29
(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);
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!