我写了一个n次点击后才触发的方法函数,然后想在vue中使用,但是发现about中this是个{}
要怎么样才能正确的拿到this呢,搜了半天没搞懂...
这是按钮
<button @click="about"></button>
这是点击的方法
about:afterNClick(()=>{
this.$alert('test')
})
这是n次点击后才触发的方法函数
//n次点击后触发
function afterNClick(fn,times=5) {
let count=0,timer=null;
return ()=>{
count++;
timer && clearTimeout(timer);
if (count>=times){
fn&&fn();
return count=0;
}
timer = setTimeout(() => {
count=0;
}, 500)
}
}
因此需要修改,执行的时机;修改到mounted里面去
当前这样写的问题是,没办法直接在 template 里面进行使用了,因此需要 在套一个方法