发布于 4年前

Error in callback for watcher

问题:

一个报错的问题处理:Error in callback for watcher "checkList": "TypeError: Cannot read property

解决方法:(箭头函数改变了指向)

handler:(val,oldVal)=>{} 这样会报错,箭头函数导致this指向出错,改成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
    }
},

上面那样写会报错,改成下面这样就可以了,不要使用箭头函数

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
    }
},
©2020 edoou.com   京ICP备16001874号-3