【发布时间】:2019-12-31 11:56:43
【问题描述】:
我在 Vue 模板中有一个简单的输入框,我想或多或少地像这样使用 debounce:
<input type="text" v-model="filterKey" debounce="500">
但是debounce 属性已变为deprecated in Vue 2。建议只说:“使用 v-on:input + 3rd party debounce function”。
你是如何正确实现它的?
我尝试使用 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)
}
过滤键随后在 computed 道具中使用。
【问题讨论】:
-
建议仔细阅读:vuejs.org/v2/guide/…
标签: vue.js vuejs2 debouncing