vue 添加項id自動獲取id


(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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM