javascript - 如何使用 Vue.js 去抖过滤器?

标签 javascript vue.js

我正在尝试根据 Vue.js docs 使用 debounce 过滤器所以我可以防止在用户输入输入时触发 Ajax 函数。过去,我曾使用 setTimeout 手动阻止在输入每个字母后发送请求并使用重置延迟,但我想以 Vue.js 的方式进行。

这是我尝试过的:

<input
v-model="myInput"
v-on="keyup: someAjaxFunction | debounce 500"
>

文档中没有专门针对此过滤器给出示例。我是否将过滤器放在了正确的位置?

debounce

this filter only works with v-on

this filter takes one optional argument

Wrap the handler to debounce it for X milliseconds, where X is the argument. Default is 300ms. A debounced handler will be delayed until at least X ms has passed after the call moment; if the handler is called again before the delay period, the delay poriod is reset to X ms.

我也试过这个:(因为文档提到“包装处理程序......”)

<input
v-model="myInput"
v-on="keyup: debounce( someAjaxFunction, 500 )"
>

我真的可以举个例子。

最佳答案

去抖动滤波器已removed .

使用lodash’s debounce (或可能限制)直接限制调用昂贵的方法。您可以像这样实现预期的结果:

<input v-on:keyup="doStuff">

methods: {
  doStuff: _.debounce(function () {
    // ...
  }, 500)
} 

来自 documentation .

关于javascript - 如何使用 Vue.js 去抖过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32441347/

相关文章:

node.js - 从 node_modules 绑定(bind)动态 Vue 图像 src

html - Vue.js v-for 不工作

javascript - 如何在Vue中加载本地json文件?

javascript - csv 到具有自定义属性的响应表

javascript - AJAX 将 Span 字符串替换为外部日期字符串

javascript - 使用不起作用的函数声明 javascript 对象属性

javascript - 在 Nuxt.js 中使用 Async/Await 等待操作完成(SPA,无 SSR)

javascript - Vuex Action 是否必须修改或使用存储状态?

javascript - jQuery 读取数据对象并等待 $.each 完成

javascript - 如何使用来自 http 请求的数组将动态输入字段添加到 Angular 形式