swiper bind click event in vue

@Click is not directly binding on the slide triggered by
the official offer on.click api method realIndex

swiperOption:{
        slidesPerView: 5,
        freeMode : true,
        freeModeMomentum : false,
        spaceBetween: 10,
        on: {
          click: function () {
            // 这里有坑,需要注意的是:this 指向的是 swpier 实例,而不是当前的 vue, 因此借助 vm,来调用 methods 里的方法 
            // 当前活动块的索引,与activeIndex不同的是,在loop模式下不会将 复制的块 的数量计算在内。
            const realIndex = this.realIndex;
            vm.handleGoods(realIndex);
          }
        }
      }

Can call, but if not, if vm in global declarations, you can bind mounted inside or created

mounted() {
    let _this = this;
    this.swiper.on('click', function () {
            // 当前活动块的索引,与activeIndex不同的是,在loop模式下不会将 复制的块 的数量计算在内。
                  const clickedIndex = this.clickedIndex;
                  _this.handleGoods(clickedIndex);
        });
  }

Note that two swiper values used, realIndex and clickedIndex
per page display multiple slide in the swiper, realIndex will be the first of a slide show of the current window swiper index, while clickedIndex will be your current click the slide of the index

Published 11 original articles · won praise 0 · Views 125

Guess you like

Origin blog.csdn.net/baiyikai/article/details/105075752