fncheng / vue-learn

0 stars 0 forks source link

在Vue中使用lodash防抖和节流踩坑 #28

Open fncheng opened 2 years ago

fncheng commented 2 years ago

Vue中使用节流

methods: {
        getSearchResult() {
            debounce(function() {
                console.log(this.searchValue);
            })();
        }
    }

发现传递的参数无论怎样也无法触发。

这其实是错误的使用方式

正确的使用方式应该是这样的:

onSave: throttle(function() {
      // 表单验证
      this.$refs['ruleForm'].validate(valid => {
        console.log('this: ', this)
        if (valid) {
          this.submitForm()
        } else return false
      })
    }, 1000, {
      leading: true
    }),