vue 中使用 lodash Debounce防抖不生效

需求:搜索按钮增加防抖功能
代码1

<template>
    <el-button  type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
</template>
  
  <script>
import { debounce } from "lodash";
// 自定义搜索按钮,带防抖
export default {
    name: "SearchButton",
    props: {
        //默认等待1秒
        wait: {
            type: Number,
            default: 1000
        }
    },
    components: {},
    data () {
        return {

        };
    },
    mounted () {
        //防抖函数
        this.debounceClick=debounce(function(){
              console.log(111111);
         },this.wait)
     },
    methods: {
        /**
         * 点击事件
         */
        searchClick () {
            this.debounceClick()
        }
        //或是
        searchClick :debounce(function(){
              console.log(111111);
         },this.wait)
    }
};
  </script>
  

参考:
https://vuejs.org/guide/essentials/reactivity-fundamentals.html#stateful-methods
在这里插入图片描述
注:debounce返回的是一个函数,debounce后面增加()或直接在点击事件中调用是不行

 searchClick () {
 	//不执行
       debounce(function(){
         console.log(111111);
         },this.wait)
 }
 searchClick () {
   //每次都执行
         debounce(function(){
              console.log(111111);
         },this.wait)()
 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值