javascript - Vue文件如何获取当前实例?
三叔
三叔 2017-06-15 09:23:36
[JavaScript讨论组]

对象里的函数如何用this获取当前实例?

export default {
  name:'calendar',
  data() {
    return {
      moment: moment(),
      month: monthArr[moment().month()],
      date: moment().date(),
      day: dayArr[moment().day()],
      swiperOption: {
        effect: 'flip',
        loop: true,
        onSlideNextEnd: function (swiper) {
          console.log('next');
          this.moment = this.moment.add(1, 'd');//这里的this不是实例的this
          this.month = monthArr[this.moment.month()];
          this.date = this.moment.date();
          this.day = dayArr[this.moment.day()];
        },
        onSlidePrevEnd: function (swiper) {
          console.log('prev;');
        }
      }
    }
  },
·
·
·
·
·
}
三叔
三叔

全部回复(2)
怪我咯

再写一个computed,把swiperOption 丢在 computed里 试下吧。这么写,实例还没创造出来,应该调用不到吧。

怪我咯
想到个方法,但是感觉挺麻烦的
data() {
    return {
      moment: moment(),
      month: monthArr[moment().month()],
      date: moment().date(),
      day: dayArr[moment().day()],
      swiperOption: {
        _this:this,
        loop: true,
        onSlideNextStart: function (swiper) {
          console.log(this._this);
          this._this.moment = this._this.moment.add(1, 'd');
          this._this.month = monthArr[this._this.moment.month()];
          this._this.date = this._this.moment.date();
          this._this.day = dayArr[this._this.moment.day()];
        },
        onSlidePrevEnd: function (swiper) {
          console.log('prev;');
        }
      }
    }
  },
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号