search

Home  >  Q&A  >  body text

How does a Vue file get the current instance?

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

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-06-15 09:25:36

    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.

    reply
    0
  • 怪我咯

    怪我咯2017-06-15 09:25:36

    想到个方法,但是感觉挺麻烦的
    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;');
            }
          }
        }
      },

    reply
    0
  • Cancelreply