问题描述:用watch监听的过程中发现,如果监听的是一个对象或者一个数组,会导致多次调用watch里面的方法,如下接口调用了两次,浪费资源。
解决方案:可以在监听watch的时候,添加一个防抖函数:
function debounce(func, delay) {
let timerId
return function(...args) {
clearTimeout(timerId)
timerId = setTimeout(() => {
func.apply(this, args)
}, delay)
}
}
// watch监听
watch: {
lineTime: {
handler: debounce(function(data) {
this.getData()
}, 1000),
immediate: false,
deep: false
}
}
以上方法可以解决watch监听导致多次调用的问题。
大家如果有好的方法也可以留言,互相学习,嘻嘻☺。