How does a Vue file get the current instance?
三叔
三叔 2017-06-15 09:23:36
0
2
664

How does the function in the object use this to get the current instance?

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;');
        }
      }
    }
  },
·
·
·
·
·
}
三叔
三叔

reply all(2)
刘奇

Write another computed and put swiperOption in computed. Give it a try. If you write it this way, the instance has not been created yet, so it should not be called.

刘奇
想到个方法,但是感觉挺麻烦的
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;');
        }
      }
    }
  },
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!