Error in callback for watcher

created at 07-03-2021 views: 1

problem

An error handling problem: 

Error in callback for watcher "checkList": "TypeError: Cannot read property

Solution: (The arrow function changes the direction)

handler:(val,oldVal)=>{} This will report an error. The arrow function causes this to point to an error. Change it to handler:function(val,oldVal){}.

watch:{
    checkList:{
        handler:(oldValue,newValue) => {
            let _sum = 0
            this.orderData.forEach(item => {
                if(this.checkList[item.order_id]){
                    _sum += item.service_price
                }
            })
            this.sum = _sum
        },
        deep:true
    }
},

If you write like the above, you will get an error, just change it to the following, don’t use arrow functions

watch:{
    checkList:{
        handler:function(oldValue,newValue){
            let _sum = 0
            this.orderData.forEach(item => {
                if(this.checkList[item.order_id]){
                    _sum += item.service_price
                }
            })
            this.sum = _sum
        },
        deep:true
    }
},
Please log in to leave a comment.