簡體   English   中英

Vue - 如何使用 lodash 去抖動

[英]Vue - How to use lodash debounce

我正在使用在 main.js 中導入的 lodash 的 debounce

import lodash from 'lodash'
Vue.prototype._ = lodash

我正在使用this._.find(...) ,一切正常。 但是,如果我使用去抖動它不起作用。

<script>
   export default {
      methods: {
        delay: this._.debounce(function () {
         // Code
        }, 500),
      }
    }
</script>

它拋出這個錯誤Uncaught TypeError: Cannot read property 'debounce' of undefined

使用this._.debounce(...)的正確方法是什么?

這應該工作

<script>
import { debounce } from 'lodash-es' // optimized es6-import package, similar to usual 'lodash'

export default {
  methods: {
    yourCoolFunction: debounce(function (event) { // event is the object parameter given to 'yourCoolFunction' if any
      // your tasty code
    }, 500),
  }
}

試試這個。 對我來說,它作為_.debounce工作,如下所示:

    <script>
       export default {
          methods: {
             delay: _.debounce(function() {
               //code
             }, 700),
          }
        }
    </script>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM