欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue以組件或者插件的形式實現(xiàn)throttle或者debounce

 更新時間:2019年05月22日 09:24:51   作者:虛光  
這篇文章主要介紹了vue以組件或者插件的形式實現(xiàn)throttle或者debounce,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

需求

在業(yè)務中,會碰到許多點擊請求的情況,在請求前改變一個lock變量(在lock變回來之前,點擊無效),在請求回調中再改變lock.以確保在,請求回來之前,不會重復請求?;蛘哳愃频狞c擊節(jié)流業(yè)務

實現(xiàn)方式

指令

 <div v-for="a in 3" :key="a" v-demo:getData="a">指令</div>
 //getData是函數(shù)名,a是傳入的參數(shù)
  directives: {
  demo: {
   bind(el: Element, binding: any, vnode: VNode) {
    const that: any = vnode.context
    // console.log(binding, vnode)
    // console.log(binding.arg, binding.value)
    if (!that[binding.arg].isBind){ // 打上標記,如果已經轉換了,就不轉了
     that[binding.arg] = deb(that[binding.arg])
     that[binding.arg].isBind = true
    }
    el.addEventListener('click', function T(event: Event): void{
     that[binding.arg](binding.value)
    })
   },
  },
 },

組件

子組件

<template>
 <div>
  <div @click="senClick">
   <slot></slot>
  </div>
 </div>
</template>
<script lang="ts">
import Vue from 'vue';
import { Component, Prop } from 'vue-property-decorator';
@Component({})
export default class Child extends Vue {
 @Prop({ type: Number, default: 500 }) public timeOut!: number; // 時間
 @Prop({ type: String, default: 'throttle' }) public type!: string; // 類型
 @Prop() public params!: any; // 傳入?yún)?shù)
 public message: string = 'Hello';
 public throttleLock: boolean = false;
 public debounceLock: number = 0;
 public time: any;
 public senClick(): void {
  console.log(this.timeOut, this.type, this.params);
  if (this.type === 'throttle') {
   if (this.throttleLock) {
    return;
   }
   this.throttleLock = true;
   setTimeout(() => {
    this.throttleLock = false;
   }, this.timeOut);
   this.$emit('myClick', this.params);
  } else if (this.type === 'debounce') {
   if (this.debounceLock) {
    clearTimeout(this.debounceLock);
   }
   this.debounceLock = setTimeout(() => {
    this.$emit('myClick', this.params);
   }, this.timeOut);
  } else {
   this.$emit('myClick', this.params);
  }
 }
}
</script>

<style scoped lang='stylus'>
div {
 width: 100%;
 height: 100%;
}
</style>

父組件

<template>
 <div class="home">
   <throttle-and-debounce @myClick="getData" :time="500" type="throttle" params="123">
    <div>我是組件內容</div>
   </throttle-and-debounce>
 </div>
</template>
import { Component, Vue } from 'vue-property-decorator';
import throttleAndDebounce from '@/components/throttleAndDebounce.vue'; 
@Component({
 components: {
  throttleAndDebounce,
 },
})
export default class home extends Vue {
public getData(e: any){
   console.log('異步數(shù)據(jù)', e)
  }
}

</script>

plugin

函數(shù)

function deb(fn: function){
 let lock: number
 return (e) => {
  if (lock){
   clearTimeout(lock)
  }
  console.log('創(chuàng)建閉包延遲執(zhí)行')
  lock = setTimeout(() => {
   fn(e)
  }, 1500)
 }
}
export {deb}

組件內使用

<template>
 <div class="home">
  <div @click="func(123)">function</div>
 </div>
</template>
<script lang="ts">
import { Component, Vue } from 'vue-property-decorator';
import {deb} from '@/assets'
@Component({
 components: {
  throttleAndDebounce,
 },
})
export default class home extends Vue {
   public beforeCreate(){
     this.func = deb((e: {a: number}) => {
     console.log('this', e)
    })
   }
}
</script>

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • Vue 中axios配置實例詳解

    Vue 中axios配置實例詳解

    本文通過實例代碼給大家介紹了Vue axios配置,非常不錯,代碼簡單易懂,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • vue使用動態(tài)添加路由(router.addRoutes)加載權限側邊欄的方式

    vue使用動態(tài)添加路由(router.addRoutes)加載權限側邊欄的方式

    這篇文章主要介紹了vue使用動態(tài)添加路由(router.addRoutes)加載權限側邊欄的方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • 在Vue中使用icon 字體圖標的方法

    在Vue中使用icon 字體圖標的方法

    這篇文章主要介紹了在Vue中使用icon 字體圖標的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-06-06
  • vue關于Promise的使用方式

    vue關于Promise的使用方式

    這篇文章主要介紹了vue關于Promise的使用方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-06-06
  • vue 微信分享回調iOS和安卓回調出現(xiàn)錯誤的解決

    vue 微信分享回調iOS和安卓回調出現(xiàn)錯誤的解決

    這篇文章主要介紹了vue 微信分享回調iOS和安卓回調出現(xiàn)錯誤的解決,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Vue2.0 實現(xiàn)歌手列表滾動及右側快速入口功能

    Vue2.0 實現(xiàn)歌手列表滾動及右側快速入口功能

    這篇文章主要介紹了Vue2.0實現(xiàn)歌手列表滾動及右側快速入口功能,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-08-08
  • VUEX?使用?mutations的兩種方式

    VUEX?使用?mutations的兩種方式

    這篇文章主要介紹了VUEX?使用?mutations的兩種方式,實現(xiàn)方式就是利用vuex中的mutations,在mutations中定義一個方法,這個方法就是把點擊的index(也就是每個列表的唯一標識),傳給state中的當前標識,需要的朋友可以參考下
    2023-01-01
  • vuex中commit和dispatch的區(qū)別解析

    vuex中commit和dispatch的區(qū)別解析

    commit 和dispatch的區(qū)別在于commit是提交mutatious的同步操作,dispatch是分發(fā)actions的異步操作,本文重點給大家講解vuex中commit和dispatch的區(qū)別,感興趣的朋友一起看看吧
    2024-06-06
  • 基于Vue的前端界面實現(xiàn)日期時間實時顯示簡單代碼

    基于Vue的前端界面實現(xiàn)日期時間實時顯示簡單代碼

    今天在項目中有一個功能是要求顯示北京的實時時間,下面這篇文章主要給大家介紹了關于如何基于Vue的前端界面實現(xiàn)日期時間實時顯示的相關資料,需要的朋友可以參考下
    2024-01-01
  • 使用Vue動態(tài)生成form表單的實例代碼

    使用Vue動態(tài)生成form表單的實例代碼

    這篇文章主要介紹了使用Vue動態(tài)生成form表單的實例代碼,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2018-04-04

最新評論