surmon-china / vue-awesome-swiper

🏆 Swiper component for @vuejs
https://github.surmon.me/vue-awesome-swiper
MIT License
12.81k stars 1.97k forks source link

using same swiper, this.swiper on one method works but the other method, it appears null. #606

Open parkyh1974 opened 4 years ago

parkyh1974 commented 4 years ago

I'm using vue-awesome swiper on SPA web page. I'm using like this in template.

<template>
...
              <swiper class="responsive"
                      :options="swiperOption"
                      ref="reportSwiper" 
                      :not-next-tick="true"
                      >
                <swiper-slide  v-for="date in slideList" v-bind:key="date">
                  <router-view  v-bind:date-info="date" ></router-view>
                </swiper-slide>  

                <div class="swiper-button-prev" slot="button-prev"></div>
                <div class="swiper-button-next" slot="button-next"></div>                
              </swiper>

...
</template>

this is how to imported.

import 'swiper/dist/css/swiper.min.css'
import { swiper, swiperSlide } from 'vue-awesome-swiper'

and I''ve set computed to use it.

  computed: {
    swiper() {
      return this.$refs.reportSwiper.swiper;
    },
}

And I'm using this.swiper at two methods.

  methods: {
    prependSlide() {
     if (this.swiper.activeIndex <= 1) {
   ....
    },
    appendSlide() {
      if ((this.swiper.activeIndex >= (this.slideList.length - 2)) {
      .....
      }
    }
}

Strange thing is, prependSlide() works as normal. but this.swiper at appendSlide() {} makes an error. the error message is

[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'activeIndex' of null"

And this.swiper appears null as alert(this.swiper);

But if I delete appendSlide() {}, it works.

How can I fix this? I really need help.

surmon-china commented 4 years ago

It is recommended to use a debugger to check the direction of this context.

In addition: it is recommended to upgrade to the latest version v4.x.