【问题标题】:Use Vue.js 3 with lodash debounce function使用带有 lodash debounce 功能的 Vue.js 3
【发布时间】:2021-01-19 00:18:17
【问题描述】:

有什么解决方案可以在方法上使用 lodash debounce 吗?我还需要函数中的“this”。 示例:

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

在 Vue2 中我可以使用:

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

【问题讨论】:

    标签: javascript vue.js vue-component vuejs3


    【解决方案1】:

    你的数据属性应该是一个返回对象的函数:

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

    并通过为去抖动回调命名来编写您的方法:

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

    【讨论】:

      猜你喜欢
      • 2022-08-13
      • 2019-04-02
      • 2017-05-05
      • 2021-02-07
      • 2019-09-01
      • 2020-12-19
      • 1970-01-01
      • 2020-06-19
      相关资源
      最近更新 更多