vue 中使用 lodash Debounce防抖不生效

打印 上一主题 下一主题

主题 972|帖子 972|积分 2916

需求:搜刮按钮增长防抖功能
代码1
  1. <template>
  2.     <el-button  type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
  3. </template>
  4.   
  5.   <script>
  6. import { debounce } from "lodash";
  7. // 自定义搜索按钮,带防抖
  8. export default {
  9.     name: "SearchButton",
  10.     props: {
  11.         //默认等待1秒
  12.         wait: {
  13.             type: Number,
  14.             default: 1000
  15.         }
  16.     },
  17.     components: {},
  18.     data () {
  19.         return {
  20.         };
  21.     },
  22.     mounted () {
  23.         //防抖函数
  24.         this.debounceClick=debounce(function(){
  25.               console.log(111111);
  26.          },this.wait)
  27.      },
  28.     methods: {
  29.         /**
  30.          * 点击事件
  31.          */
  32.         searchClick () {
  33.             this.debounceClick()
  34.         }
  35.         //或是
  36.         searchClick :debounce(function(){
  37.               console.log(111111);
  38.          },this.wait)
  39.     }
  40. };
  41.   </script>
  42.   
复制代码
参考:
https://vuejs.org/guide/essentials/reactivity-fundamentals.html#stateful-methods

注:debounce返回的是一个函数,debounce后面增长()或直接在点击事件中调用是不可
  1. searchClick () {
  2.         //不执行
  3.        debounce(function(){
  4.          console.log(111111);
  5.          },this.wait)
  6. }
复制代码
  1. searchClick () {
  2.    //每次都执行
  3.          debounce(function(){
  4.               console.log(111111);
  5.          },this.wait)()
  6. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

冬雨财经

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表