在 Vuejs 组件中使用 Lodash 编写异步函数 [英] Write async function with Lodash in a Vuejs component
本文介绍了在 Vuejs 组件中使用 Lodash 编写异步函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个函数需要编写异步,但我无法以正确的方式完成.希望大家帮忙.
I have a function need to write async but I can't do it the right way. Hope your guys help.
async search (loading, search, vm) {
let vm = this
_.debounce(() => {
let ApiURL = '/users/'
}
let { res } = await api.get(ApiURL) //Error
vm.options = res.data
}, 800)
推荐答案
直接将lodash函数赋值为组件方法即可
Just assign the lodash function directly as a component method
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/
就你而言:
methods: {
search: _.debounce(async function () {
// this code may differ from your actual implementation
const res = await api.get('/users/')
this.options = res.data
}, 1000)
}
}
这篇关于在 Vuejs 组件中使用 Lodash 编写异步函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文