我似乎无法去除 (lodash) 计算属性或 vuex getter。去抖功能总是返回未定义。
https://jsfiddle.net/guanzo/yqk0jp1j/2/
HTML:
<div id="app">
<input v-model="text">
<div>computed: {{ textComputed }} </div>
<div>debounced: {{ textDebounced }} </div>
</div>
记者:
new Vue({
el:'#app',
data:{
text:''
},
computed:{
textDebounced: _.debounce(function(){
return this.text
},500),
textComputed(){
return this.text
}
}
})
原文由 Eric Guan 发布,翻译遵循 CC BY-SA 4.0 许可协议
正如我在评论中提到的,去抖动本质上是一种异步操作,因此无法返回值。根据您的需要,您可能希望在 输入 端进行去抖动。 —中的值与
text
textComputed
的值没有区别,但如果你v-model="textComputed"
,值设置将被去抖动。如果你特别想要一个变量的去抖动版本, mrogers 已经给出了一个很好的解决方案。