vue之input輸入框防抖debounce函數(shù)的使用方式
方法一
這種方式很簡(jiǎn)單,但是能實(shí)現(xiàn)一樣的功能。
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項(xiàng)目名稱" v-model="searchValue" @keyup.enter="searchBtn" @input="searchBtn">
</div>
</template><script>
let timers = null;
export default{
data(){
return{
searchValue:''
}
}
methods:{
searchBtn(){
clearTimeout(timers)
timers = setTimeout(() => {
this.getOfferList()//需要防抖的函數(shù)
}, 500);
},
}
}
</script>方法二
這個(gè)方法是vue官網(wǎng)上的做法。
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項(xiàng)目名稱" v-model="searchValue">
</div>
<template><script>
export default{
data(){
return{
searchValue:''
}
}
//監(jiān)聽(tīng)input輸入值變化
watch:{
searchValue:function(){
this.debouncedGetAnswer();
}
},
created(){
this.debouncedGetAnswer = this.debounce(this.getOfferList, 500);
//this.getOfferList是你查詢數(shù)據(jù)的函數(shù),也就是需要防抖的函數(shù)
},
methods:{
//防抖
debounce(fn, delay = 500){
let timer = null;
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
}
</script>方法三
1.封裝一個(gè)模塊,引入即可,在utils新建一個(gè)js文件,名稱隨便
<template>
<div>
<input class="inputBox" type="text" placeholder="搜索項(xiàng)目名稱" v-model="searchValue">
</div>
<template><script>
export default{
data(){
return{
searchValue:''
}
}
//監(jiān)聽(tīng)input輸入值變化
watch:{
searchValue:function(){
this.debouncedGetAnswer();
}
},
created(){
this.debouncedGetAnswer = this.debounce(this.getOfferList, 500);
//this.getOfferList是你查詢數(shù)據(jù)的函數(shù),也就是需要防抖的函數(shù)
},
methods:{
//防抖
debounce(fn, delay = 500){
let timer = null;
return function() {
if (timer) {
clearTimeout(timer)
}
timer = setTimeout(() => {
fn.apply(this, arguments)
timer = null
}, delay)
}
}
}
</script>2.在需要的vue文件引入即可使用
import { _throttle, _debounce } from '@/utils/throttle'3.在vue文件的使用方法
// 監(jiān)聽(tīng)搜索框內(nèi)容的變化,等輸入完成才會(huì)執(zhí)行搜索函數(shù)=>即防抖
watch: {
searchValue: _debounce(function() {
this.page = 1
this.getJobFairList()
})
},
// 搜索,短時(shí)間內(nèi)連續(xù)點(diǎn)擊搜索按鈕只執(zhí)行一次搜索函數(shù)=>即節(jié)流
searchBtn: _throttle(function() {
if (this.searchValue) {
this.getJobFairList()
}
}),方法三是最近才更新的代碼,也是最新的代碼,建議使用方法三
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
- Vue中輸入框僅支持?jǐn)?shù)字輸入的四種方法
- vue如何設(shè)置輸入框只能輸入數(shù)字且只能輸入小數(shù)點(diǎn)后兩位,并且不能輸入減號(hào)
- vue elementui 動(dòng)態(tài)追加下拉框、輸入框功能
- 基于vue3實(shí)現(xiàn)一個(gè)簡(jiǎn)單的輸入框效果
- Vue中禁止編輯的常見(jiàn)方法(以禁止編輯輸入框?yàn)槔?
- vue實(shí)現(xiàn)輸入框只允許輸入數(shù)字
- vue3+elementUI實(shí)現(xiàn)懸浮多行文本輸入框效果
- 基于vue+h5實(shí)現(xiàn)車(chē)牌號(hào)輸入框功能(demo)
相關(guān)文章
vue完美實(shí)現(xiàn)el-table列寬自適應(yīng)
這篇文章主要介紹了vue完美實(shí)現(xiàn)el-table列寬自適應(yīng),對(duì)vue感興趣的同學(xué),可以參考下2021-05-05
vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量
這篇文章主要給大家介紹了關(guān)于vue3實(shí)戰(zhàn)教程之a(chǎn)xios的封裝和環(huán)境變量的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02
Vue Element使用icon圖標(biāo)教程詳解(第三方)
element-ui自帶的圖標(biāo)庫(kù)不夠全,還是需要需要引入第三方icon。下面小編給大家?guī)?lái)了Vue Element使用icon圖標(biāo)教程,感興趣的朋友一起看看吧2018-02-02
使用Vuex實(shí)現(xiàn)一個(gè)筆記應(yīng)用的方法
這篇文章主要介紹了使用Vuex實(shí)現(xiàn)一個(gè)筆記應(yīng)用的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue3?ref實(shí)現(xiàn)響應(yīng)式的方法
這篇文章主要介紹了vue3的ref是如何實(shí)現(xiàn)響應(yīng)式的,我們講了ref是如何實(shí)現(xiàn)響應(yīng)式的,主要分為兩種情況:ref接收的是number這種原始類型、ref接收的是對(duì)象這種非原始類型,需要的朋友可以參考下2024-07-07
vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解
這篇文章主要介紹了vue學(xué)習(xí)筆記之指令v-text && v-html && v-bind詳解,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-05-05

