關於vue中使用lodash的防抖用法


需求:手風琴效果(mouseover的函數防抖)

 

 

 

 

1.安裝: npm install --save lodash

2.引入:import debounce from "lodash.debounce";

3.使用:

 1       <div class="home_box">
 2         <div
 3           class="home_menu"
 4           v-for="(item, idx) in homeMenu"
 5           :key="idx"
 6           :class="{ active: hoverFlag === idx }"
 7 @mouseover="overevent(idx)"
 8         >
 9           <p :class="item.icon" class="home_menu_title">{{ item.label }}</p>
10           <ul class="home_menu_list">
11             <li
12               v-for="(item1, idx1) in item.child"
13               :key="idx1"
14               :class="item1.class"
15               @click="routerLink(item1.url)"
16             >
17               <img :src="item1.img" alt="" />
18             </li>
19           </ul>
20         </div>
21       </div>

 

可以有不同的寫法:

 1   methods: {
 2     // 鼠標滑入事件
 3     overevent(idx) {
 4         this.enterEvent(idx)
 5     },
 6     enterEvent: 
 7         debounce(function(idx) {
 8             this.hoverFlag = idx;
 9         }, 200),
10 
11   },

或者

1   methods: {
2     overevent: 
3         debounce(function(idx) {
4             this.hoverFlag = idx;
5         }, 100),
6 
7   },

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM