vue中使用loadsh的debounce防抖函數(shù)問(wèn)題
使用loadsh的debounce防抖函數(shù)
<s-input placeholder="請(qǐng)輸入查詢關(guān)鍵字" prefix-icon="magnifier" v-model="content"></s-input>
<script>
import {debounce} from 'lodash'
export default {
watch: {
content: debounce(function(newVal, oldVal) {
this.search()
}, 1000)
},
methods:{
search(){
$axios.post('').then((res) => {})
}
}
}
</script>
<s-button @click="handleSave"></s-button>
<script>
import {debounce} from 'lodash'
export default {
methods:{
handleSave: debounce(function() {
//執(zhí)行方法
},1000),
}
}
</script>
debounce防抖函數(shù)的使用(js和vue)
debounch防抖函數(shù)主要應(yīng)用場(chǎng)景,解決按鈕快速點(diǎn)擊,多次請(qǐng)求的問(wèn)題和input輸入框多次請(qǐng)求的問(wèn)題。接下來(lái)直接介紹在js和vue項(xiàng)目中的使用方式。
lodash工具庫(kù)地址:https://lodash.com/docs#debounce
三個(gè)參數(shù):
_.debounce(func, [wait=0], [options={}])options 有如下三個(gè)可選參數(shù)配置:
leading:指定在延遲開(kāi)始前是否執(zhí)行 func(默認(rèn)為 false)。maxWait:設(shè)置 func 允許被延遲的最大值。trailing:指定在延遲結(jié)束后是否執(zhí)行 func(默認(rèn)為 true)
1.在js項(xiàng)目中的使用
(1)引用lodash工具庫(kù):
<script src="https://cdn.jsdelivr.net/npm/lodash@4.13.1/lodash.min.js"></script>
(2)代碼:html:<div class="ceshi">點(diǎn)擊測(cè)試</div>
js:使用jq需引入jq庫(kù),否則報(bào)錯(cuò)
$(".ceshi").on("click",function(){
showAlert()
})
let showAlert = _.debounce(ceshiFun,400,
{'leading': true,
'trailing': false})
function ceshiFun(){
console.log("測(cè)試")
}2.在vue項(xiàng)目中的使用
(1)安裝。進(jìn)入項(xiàng)目文件夾,執(zhí)行如下命令使用 npm 安裝 lodash:npm i --save lodash
(2)引入項(xiàng)目:import _ from 'lodash'
(3)在項(xiàng)目中使用示例:
<template>
<div id="test">
<button @click="btnClick">點(diǎn)擊</button>
</div>
</template>
<script>
import _ from 'lodash';
export default {
name: 'Test',
data () {
return {
}
},
methods:{
//按鈕點(diǎn)擊
btnClick() {
//顯示消息
this.showAlert('歡迎訪問(wèn)hangge.com');
},
// 顯示消息時(shí)增加防抖(500毫秒延遲)
showAlert: _.debounce(function(message){
alert(message);
}, 500)
},
// 頁(yè)面創(chuàng)建時(shí)自動(dòng)加載數(shù)據(jù)
created() {
}
}
</script>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue.js配合$.post從后臺(tái)獲取數(shù)據(jù)簡(jiǎn)單demo分享
今天小編就為大家?guī)?lái)一篇vue.js配合$.post從后臺(tái)獲取數(shù)據(jù)簡(jiǎn)單demo分享,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
詳解vue2.0+vue-video-player實(shí)現(xiàn)hls播放全過(guò)程
這篇文章主要介紹了詳解vue2.0+vue-video-player實(shí)現(xiàn)hls播放全過(guò)程,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
vue中v-for和v-if一起使用之使用compute的示例代碼
這篇文章主要介紹了vue中v-for和v-if一起使用之使用compute的相關(guān)知識(shí),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
解決Vue警告Write?operation?failed:computed?value?is?readonl
這篇文章主要給大家介紹了關(guān)于如何解決Vue警告Write?operation?failed:computed?value?is?readonly的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03
vue項(xiàng)目前端微信JSAPI與外部H5支付相關(guān)實(shí)現(xiàn)過(guò)程及常見(jiàn)問(wèn)題
這篇文章主要介紹了vue項(xiàng)目前端微信JSAPI與外部H5支付相關(guān)實(shí)現(xiàn)過(guò)程及常見(jiàn)問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04

