账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue中的回调函数this指针问题。
    29
    0

    问题:vue.js项目中使用回调函数的时候,this指针会指向空?

    代码如下:

    /**
     * @desc: 节流函数
     * @date: 2019/5/3
     */
    
    console.log(this);   // 结果1
    function _throttle (callback, timeout = 3000) {
      let lastTime = 0;
      let that = this;
      console.log(this, callback);  // 结果2
      return function (...args) {
        if (+new Date() - lastTime >= timeout) {
          console.log(that, this, args);  // 结果3
          callback.call(this, this, args)
          lastTime = +new Date()
        }
      }
    }
    
    export {
        _throttle
    }
    

    使用该函数:

    组件: @click="handlePraise(praiseClo)"  // praiseClo: 57
    
    import { _throttle } from 'src/common/utils'
    
    handlePraise: _throttle((that, args) => {
          console.log(that, args);  // 结果5
        }, 3000)

    思路: 有没有更有效的方案传递this ??

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    • 夢でも 普通会员 1楼
      502 Bad Gateway

      502 Bad Gateway


      nginx
    更多回答
    网站公告
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部