utils.js
const throttle = (fn, gapTime, ctx) => {
let lock = true
return function(){
if(!lock) return
lock = false
setTimeout(() => {
fn.apply(this, arguments)
lock = true
}, gapTime)
}
}
export default { throttle }
然后在vue文件中引入 utils.js 文件
import throttle from './script/utils'
methods: {
throttleBtn:throttle(function(e){
console.log('我被点击了~')
},1000)
}
看起来没问题,然后报错了
最后把调用地方改成这样就可以了
import utils from './script/utils'
methods: {
throttleBtn:utils.throttle(function(e){
console.log('我被点击了~')
},1000)
}
分许:通过 import { throttle } from './script/utils' 引入的默认是对象而不是函数。