The useSwiper composable provides a reactive interface to a <swiper-container> element. It allows you to access the underlying Swiper instance, monitor its state (like current index or progress), and trigger navigation methods (like next, prev, or to) directly from your Vue component logic.
Parameters
swiperContainerRef: A Vue Ref pointing to the <swiper-container> element.options (optional): An object containing Swiper configuration parameters. If provided, these options are automatically applied to the container and the Swiper instance is initialized.
Returned Properties
Instance
instance: A Ref containing the actual Swiper instance.
Reactive State
These properties update automatically as the slider moves:
isBeginning: true if the slider is at the first slide.isEnd: true if the slider is at the last slide.activeIndex: The index of the currently active slide.realIndex: The index of the currently active slide (useful when using loop: true).slides: An array of the current slides.slidesPerView: The number of slides visible.progress: The current progress of the slider.getNumberOfSlides: The total number of slides.
Navigation Methods
next(...params): Transitions to the next slide.prev(...params): Transitions to the previous slide.to(...params): Transitions to a specific slide index.reset(...params): Resets the swiper position to the currently active slide.reInitialize(): Re-runs the initialization logic.
<script setup>
import { ref } from 'vue'
const swiperRef = ref(null)
const { next, prev, isBeginning, isEnd } = useSwiper(swiperRef, {
slidesPerView: 1,
spaceBetween: 10,
})
</script>
<template>
<swiper-container ref="swiperRef">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
</swiper-container>
<button @click="next" :disabled="isEnd">Next</button>
<button @click="prev" :disabled="isBeginning">Previous</button>
</template>