Migrating From ion-slides to Swiper.js
We recommend Swiper.js if you need a modern touch slider component. It powers our ion-slides
component, but we now recommend that developers use Swiper for Vue directly.
This guide will go over how to get Swiper for Vue set up in your Ionic Framework application. It will also go over any migration information you may need to move from ion-slides
to the official Swiper Vue integration.
Swiper's Vue component is set to be removed in a future release of Swiper, with Swiper Element as the replacement. However, this guide shows how to migrate to the Vue component because it provides the most stable experience at the time of writing.
Using Swiper's Vue component is not required to use Swiper.js with Ionic Framework.
Getting Started
First, update to the latest version of Ionic:
npm install @ionic/vue@latest @ionic/vue-router@latest
We recommend upgrading to Vue CLI 5 for better compatibility with Swiper:
vue upgrade --next
Once that is done, install the Swiper dependency in your project:
npm install swiper@latest
Swiping with Style
Next, we need to import the base Swiper styles. We are also going to import the styles that Ionic provides which will let us customize the Swiper styles using the same CSS Variables that we used with ion-slides
.
We recommend importing the styles in the component in which Swiper is being used. This ensures that the styles are only loaded when needed:
<script>
import { defineComponent } from 'vue';
import 'swiper/css';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
...
});
</script>
Importing @ionic/vue/css/ionic-swiper.css
is not required to use Swiper.js with Ionic. This files is used for backward-compatibility with the ion-slides
component and can be safely omitted if you prefer not to use the CSS Variables provided in the stylesheet.
Updating Selectors
Previously, we were able to target ion-slides
and ion-slide
to apply any custom styling. The contents of those style blocks remain the same, but we need to update the selectors. Below is a list of selector changes when going from ion-slides
to Swiper Vue:
ion-slides Selector | Swiper Selector |
---|---|
ion-slides | .swiper |
ion-slide | .swiper-slide |
Pre-processors (optional)
For developers using SCSS or Less styles, Swiper also provides imports for those files.
For Less styles, replace css
with less
in the Swiper import path:
import 'swiper/less';
import '@ionic/vue/css/ionic-swiper.css';
For SCSS styles replace css
with scss
in the Swiper import path:
import 'swiper/scss';
import '@ionic/vue/css/ionic-swiper.css';
Using Components
Swiper exports two components: Swiper
and SwiperSlide
. The Swiper
component is the equivalent of IonSlides
, and SwiperSlide
is the equivalent of IonSlide
.
These components are imported from swiper/vue
and provided to your Vue component:
<template>
<ion-page>
<ion-content>
<swiper>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
</ion-content>
</ion-page>
</template>
<script>
import { defineComponent } from 'vue';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { IonContent, IonPage } from '@ionic/vue';
import 'swiper/css';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
components: {
Swiper,
SwiperSlide,
IonContent,
IonPage,
},
});
</script>
Using Modules
By default, Swiper for Vue does not import any additional modules. To use modules such as Navigation or Pagination, you need to import them first.
ion-slides
automatically included the Pagination, Scrollbar, Autoplay, Keyboard, and Zoom modules. This part of the guide will show you how to install these modules.
To begin, we need to import the modules and their corresponding CSS files from the swiper
package:
<template>
<ion-page>
<ion-content>
<swiper>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
</ion-content>
</ion-page>
</template>
<script>
import { defineComponent } from 'vue';
import { Autoplay, Keyboard, Pagination, Scrollbar, Zoom } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { IonContent, IonPage } from '@ionic/vue';
import 'swiper/css';
import 'swiper/css/autoplay';
import 'swiper/css/keyboard';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
import 'swiper/css/zoom';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
components: { Swiper, SwiperSlide, IonContent, IonPage },
});
</script>
From here, we need to provide these modules to Swiper by using the modules
property on the swiper
component:
<template>
<ion-page>
<ion-content>
<swiper :modules="modules">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
</ion-content>
</ion-page>
</template>
<script>
import { defineComponent } from 'vue';
import { Autoplay, Keyboard, Pagination, Scrollbar, Zoom } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { IonContent, IonPage } from '@ionic/vue';
import 'swiper/css';
import 'swiper/css/autoplay';
import 'swiper/css/keyboard';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
import 'swiper/css/zoom';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
components: { Swiper, SwiperSlide, IonContent, IonPage },
setup() {
return {
modules: [Autoplay, Keyboard, Pagination, Scrollbar, Zoom],
};
},
});
</script>
Finally, we can turn these features on by using the appropriate properties:
<template>
<ion-page>
<ion-content>
<swiper :modules="modules" :autoplay="true" :keyboard="true" :pagination="true" :scrollbar="true" :zoom="true">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
</ion-content>
</ion-page>
</template>
<script>
import { defineComponent } from 'vue';
import { Autoplay, Keyboard, Pagination, Scrollbar, Zoom } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { IonContent, IonPage } from '@ionic/vue';
import 'swiper/css';
import 'swiper/css/autoplay';
import 'swiper/css/keyboard';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
import 'swiper/css/zoom';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
components: { Swiper, SwiperSlide, IonContent, IonPage },
setup() {
return {
modules: [Autoplay, Keyboard, Pagination, Scrollbar, Zoom],
};
},
});
</script>
See https://swiperjs.com/vue#usage for a full list of modules.
The IonicSlides Module
With ion-slides
, Ionic automatically customized dozens of Swiper properties. This resulted in an experience that felt smooth when swiping on mobile devices. We recommend using the IonicSlides
module to ensure that these properties are also set when using Swiper directly. However, using this module is not required to use Swiper.js in Ionic.
It is recommended to review the properties set by IonicSlides
and determine which ones you would like to customize.
We can install the IonicSlides
module by importing it from @ionic/vue
and passing it in as the last item in the modules
array:
<template>
<ion-page>
<ion-content>
<swiper :modules="modules" :autoplay="true" :keyboard="true" :pagination="true" :scrollbar="true" :zoom="true">
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
</swiper>
</ion-content>
</ion-page>
</template>
<script>
import { defineComponent } from 'vue';
import { Autoplay, Keyboard, Pagination, Scrollbar, Zoom } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import { IonContent, IonPage, IonicSlides } from '@ionic/vue';
import 'swiper/css';
import 'swiper/css/autoplay';
import 'swiper/css/keyboard';
import 'swiper/css/pagination';
import 'swiper/css/scrollbar';
import 'swiper/css/zoom';
import '@ionic/vue/css/ionic-swiper.css';
export default defineComponent({
components: { Swiper, SwiperSlide, IonContent, IonPage },
setup() {
return {
modules: [Autoplay, Keyboard, Pagination, Scrollbar, Zoom, IonicSlides],
};
},
});
</script>
The IonicSlides
module must be the last module in the array. This will let it automatically customize the settings of modules such as Pagination, Scrollbar, Zoom, and more.
Properties
Swiper options are provided as props directly on the <swiper>
component rather than via the options
object in ion-slides
.
Let's say in an app with ion-slides
we had the slidesPerView
and loop
options set:
<template>
<ion-slides :options="{ slidesPerView: 3, loop: true }">
<ion-slide>Slide 1</ion-slide>
<ion-slide>Slide 3</ion-slide>
<ion-slide>Slide 3</ion-slide>
</ion-slides>
</template>