我监听的是props里面的父组件传过来的form。直接上代码:
1.methods里面
methods: {
debounce: function(fn, wait) { // 防抖函数
if (this.fun !== null) {
clearTimeout(this.fun);
}
this.fun = setTimeout(fn, wait);
},
async getClassifyData(){ // 我请求的函数
const res = await classify(this.categoryId,this.newVal)
const resData = res.data
if(resData.code == '200') {
this.$emit('titleChange',resData.data)
}
}
}
2.watch监听
watch: {
// 监听form里面的title的变化,并发送请求
"form.title": {
handler(newVal) {
if(newVal){
this.newVal = newVal
this.debounce(this.getClassifyData,1000)
}
},
deep:true
},
}