Vue 中使用 Lodash throttle/debounce 不生效问题

错误用法

methods: {
    throttleHandler(){
      _.throttle(function () {
          console.log('节流');
          this.submit()
     }, 1000)
}

正确用法

方式一:该方法在 Vue 常规写法中,没有问题。也是网上能搜索到最多的用法,如下:

methods: {
    throttleHandler: _.throttle(function () {
      console.log('节流');
      this.submit()
    }, 1000),

方式二:在使用 vue-property-decorator 装饰器模式开发的时候,方式一就报错了。可以使用下面这种方式

throttleHandler(e) {
      _.throttle(function() {
        console.log('节流');
        this.submit()
      }, 1000).call(this);
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容