Error-in-callback-for-watcher

19次阅读

共计 563 个字符,预计需要花费 2 分钟才能阅读完成。

问题:

一个报错的问题处理: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
    }
},

正文完
 0