question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SPA mode get swiper instance undefined

See original GitHub issue

Vue.js version 2.3.4, component version 2.6.7, swiper 3.2.4

I can’t get swiper instance by computed

example code below

<template>
<swiper class="honor-carousel" :options="config" ref="honorSwiper">
  <swiperSlide
    v-for="(item, idx) in honorList"
    :key="idx">
    <div class="honor-item">
      <div class="img">
        <img :src="item.img" alt="">
      </div>
      <div class="text">{{ item.title }}</div>
    </div>
  </swiperSlide>
</swiper>
</template>

<script>
import { swiper, swiperSlide } from 'vue-awesome-swiper'
import { honorList } from './data'
export default {
  name: 'honor',
  components: {
    swiper,
    swiperSlide
  },
  data() {
    return {
      config: {
        pagination: '.swiper-pagination',
        spaceBetween: 14,
        slidesPerView: 1,
        loop: true,
        loopAdditionalSlides: 10,
				initialSlide: 1,
        paginationClickable: true,
				grabCursor: true,
        onSlideChangeEnd: this.check
      },
      honorList
    }
  },
  computed: {
    __swiper__() {
      return this.$refs.honorSwiper.swiper
    }
  },
  methods: {
    check() {
      console.log(this.__swiper__)
    }
  },
  mounted() {
    console.log(this.__swiper__)
  }
}
</script>

there is no async process in this example. the check methods and mounted methods output undefined both, what should i do to get the swiper instance? hope to get your reply, thx.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Xujkstracommented, Dec 27, 2017

The right approach is add this.$nextTick in the callback.

check() {
   this.$nextTick(() => {
     console.log(this.swiperInstance)
    })
 }
0reactions
ghostcommented, Dec 25, 2018

so, to return the swiper is not a good idea?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't access swiper js instance of a glider - WordPress.org
swiperInstance = imageCarousel.data( 'swiper' );. I would need to make it work also for the glider. for some reason I get undefined for...
Read more >
ScrollTrigger seem to fail randomly + other issues - GreenSock
Cannot read properties of undefined. I'm getting an error when loading a page when you don't start at the "top" of the page,...
Read more >
Why is my swiper undefined? - Stack Overflow
This is because of scoping issue. inside the click handler, the scope is changed and it does not have access to innerSwiper.
Read more >
The $q object - Quasar Framework
Reference to Capacitor global object. Available only when running under a Capacitor app. Usage. The following sections will teach you how to use...
Read more >
Jmol/JSmol Interactive Script Documentation
in order to switch into "math mode" temporarily to obtain a variable, ... Use of undefined variables in Jmol is not an error;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found