vue防抖函数怎么使用?

本篇内容主要讲解“vue防抖函数怎么使用”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“vue防抖函数怎么使用”吧!

1.首先,新建一个utils.js文件,并在文件中定义一个防抖函数;


export default {
debounce(fn, delay = 300) {
var timer;
return function() {
var args = arguments;
if(timer) {
clearTimeout(timer);
}
timer = setTimeout(() => {
fn.apply(this, args);
}, delay);
};
}
}

2.防抖函数定义好后,在vue-cli中创建一个vue.js项目;


vue create project-name

3.vue.js项目创建好后,在项目中使用import方法引入utils.js文件;


import utils from "@/js/utils";

4.最后,引入utils.js文件后,使用methods方法即可调用防抖函数;


export default {
methods: {
search: utils.debounce(function() {
let v = this;
let serchURL = `/movie/search?q=${v.searchText}&start=0&count=10`;
v.$axios
.get(serchURL)
.then(response => {
v.processSearchData(response.data);
})
.catch(error => {
console.log(error)
})
.finally()
})
}
}

到此,相信大家对“vue防抖函数怎么使用”有了更深的了解,不妨来实际操作一番吧!这里是***网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!