To utilize the lodash function as a component method, simply assign it directly.
new Vue({
el: '#app',
data: { requests: 0 },
methods: {
search: _.throttle(async function () {
const res = await fetch('/echo/json/')
this.requests++
console.log(res)
}, 1000)
},
created () {
// 100ms interval but throttle works at 1000ms
setInterval(() => {
this.search()
}, 100)
}
})
https://jsfiddle.net/6thcxfym/
If you are facing a similar scenario:
methods: {
search: _.debounce(async function () {
// this code may differ from your actual implementation
const res = await api.get('/users/')
this.options = res.data
}, 1000)
}
}