简体   繁体   English

使用带有 lodash 去抖动功能的 Vue.js 3

[英]Use Vue.js 3 with lodash debounce function

Is there any solution to use lodash debounce on method?是否有任何解决方案可以在方法上使用 lodash 去抖动? I also need 'this' in the function.我还需要在函数中使用“this”。 Example:例子:

data() {
    info: 'Read Me!'
},
methods: {
  readData() {
      console.log(this.info)
  }
}

In Vue2 I could use:在 Vue2 中,我可以使用:

methods: {
  readData: debounce(function() {
      console.log(this.info)
  }, 500)
}

Your data property should be a function that returns an object :您的数据属性应该是一个返回对象的函数:

data() {
   return{
    info: 'Read Me!'
   }
},

and write your method by giving a name to the debounce callback :并通过为 debounce 回调命名来编写您的方法:

methods: {
  readData: debounce(function debounceRead() {
      console.log(this.info)
  }, 500)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM