I can't seem to debounce (lodash) computed properties or vuex getters. The debounced functions always return undefined.
https://jsfiddle.net/guanzo/yqk0jp1j/2/
HTML:
<div id="app">
<input v-model="text">
<div>computed: {{ textComputed }} </div>
<div>debounced: {{ textDebounced }} </div>
</div>
JS:
new Vue({
el:'#app',
data:{
text:''
},
computed:{
textDebounced: _.debounce(function(){
return this.text
},500),
textComputed(){
return this.text
}
}
})
As I mention in my comment, debouncing is an inherently asynchronous operation, and so cannot return a value. Depending on your needs, you might want to debounce on the input side. There will be no difference between the value in
text
and that intextComputed
, but if youv-model="textComputed"
, the value setting will be debounced.If you specifically want a debounced version of a variable, mrogers has given a good solution.
_.debounce
)I don't have any insight as to why the debounce function doesn't work on a computed property. However, an alternative solution is to put the debounce on a function in the
methods
section and call it via awatch
.https://jsfiddle.net/vsc4npv3/
HTML:
JavaScript: