VUE中使用lodash的debounce和throttle方法

偶尔善良 提交于 2019-12-24 00:18:14

说明:

debounce和throttle在脚手架的使用,此处以防抖函数debounce为例避免按钮被重复点击

引入:

import lodash from 'lodash'

使用:

方法一:直接使用debounce方法

 //  审核
      audit: lodash.debounce(function() {
          this.$refs['model'].saveTotalResult(1).then(() => {
            const reportId = this.activeReport.id;
            report.audit(reportId).then(res => {
				 successMsg(res.msg)
            })
          })
        }, 300),

方式二:方法内部调用,先声明后使用

	audit() {
        this.auditLoading = true;
        // 需要使用箭头函数避免this指向问题(此处考虑按钮加载状态所以将loading放在debounce外部)
        const audit = lodash.debounce(() => {
          this.$refs['model'].saveTotalResult(1).then(() => {
            const reportId = this.activeReport.id;
            report.audit(reportId).then(res => {
              this.auditLoading = false;
              successMsg(res.msg)
            })
          })
        }, 300)
        audit();
      },
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!