How to implement debounce in Vue2?

前端 未结 13 663
梦谈多话
梦谈多话 2020-11-27 11:08

I have a simple input box in a Vue template and I would like to use debounce more or less like this:



        
13条回答
  •  粉色の甜心
    2020-11-27 11:26

    Although pretty much all answers here are already correct, if anyone is in search of a quick solution I have a directive for this. https://www.npmjs.com/package/vue-lazy-input

    It applies to @input and v-model, supports custom components and DOM elements, debounce and throttle.

    Vue.use(VueLazyInput)
      new Vue({
        el: '#app', 
        data() {
          return {
            val: 42
          }
        },
        methods:{
          onLazyInput(e){
            console.log(e.target.value)
          }
        }
      })
    
    
     
    
    
    {{val}}

提交回复
热议问题