上代码
main.js
//
import { createApp } from 'vue'
import App from './App.vue'
const app = createApp(App)
app.config.globalProperties.debounce = function (fun, delay) {
return function (...args) {
clearTimeout(fun.id)
fun.id = setTimeout(() => {
fun.call(this, args)
}, delay)
}
}
app.mount('#app')
组件调用
<template>
<div class="hello">
<button @click="clickFun($event,['aaaa', 'bbbb'])">点击</button>
</div>
</template>
//
import { getCurrentInstance } from 'vue';
export default {
name: 'HelloWorld',
setup() {
const { proxy: _this } = getCurrentInstance()
function callbackFun(params) {
console.log(params)
}
console.log(_this)
const clickFun = _this.debounce(callbackFun, 500)
return {
clickFun,
callbackFun
}
}
}