我在 Vue 模板中有一个简单的输入框,我想或多或少地像这样使用 debounce:
<input type="text" v-model="filterKey" debounce="500">
但是 debounce
属性已 在 Vue 2 中被弃用。建议只说:“使用 v-on:input + 3rd 方去抖功能”。
您如何正确实施它?
我尝试使用 lodash 、 v-on:input 和 v-model 来实现它,但我想知道是否可以不使用额外的变量。
在模板中:
<input type="text" v-on:input="debounceInput" v-model="searchInput">
在脚本中:
data: function () {
return {
searchInput: '',
filterKey: ''
}
},
methods: {
debounceInput: _.debounce(function () {
this.filterKey = this.searchInput;
}, 500)
}
filterkey 稍后在 computed
道具中使用。
原文由 MartinTeeVarga 发布,翻译遵循 CC BY-SA 4.0 许可协议
我正在使用 debounce NPM 包并像这样实现:
使用 lodash 和问题中的示例,实现如下所示: