gpt4 book ai didi

vue.js - 使用 Lodash 去抖 Vue 组件方法

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:20 25 4
gpt4 key购买 nike

我正在尝试使用 Lodash 的 debounce在 Vue 2 方法上,以便仅在用户停止在输入字段中键入时运行,但我得到了意想不到的结果:

输入字段

<input type="text" v-model='filter.user' placeholder="search" @keyup='dTest'>

方法

 dTest() {
const d = _.debounce(() => {
console.log('hi');
}, 2000);
d();
}

但是,每次按键都会将“hi”记录到控制台,并有两秒的延迟。

谢谢

最佳答案

dTest 更改为:

dTest = _.debounce(() => {
console.log('hi');
}, 2000);

使用 dTest,每次运行 dTest 时都会创建一个新的去抖动函数。您应该只创建此函数一次,如上所示,然后每次都调用该函数。

关于vue.js - 使用 Lodash 去抖 Vue 组件方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42329061/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com