我知道对方法使用箭头语法将导致'this'不被映射到Vue实例,但是在我的示例中,我使用lodash.debounce,据我所知,我没有使用这里的箭头语法?
返回无法读取未定义的属性'$ emit'
methods: {
onChange: debounce(function(el) {
this.$emit('change', el.target.value)
}, 1000)
}
我在编写不带箭头符号的防抖动功能时是否存在语法错误?
您的代码应该可以正常工作。您能否提供其他背景信息?
略有改进:通常最好为每个实例创建去抖动的功能,而不是在该组件的所有实例之间共享一个功能。
methods: {
onChange(e) {
this.$emit('change', e.target.value)
}
},
created() {
this.onChange = _.debounce(this.onChange, 1000)
}
您的问题是this
绑定到函数本身,而不绑定到vue实例。使用箭头功能代替
methods: {
onChange: debounce((el) => {
this.$emit('change', el.target.value)
}, 1000)
}