Vue 2 Wrapper for Slick Carousel
vue-slick is a component library that provides a Vue 2 wrapper for the popular jQuery-based Slick Carousel. It allows developers to integrate the feature-rich Slick carousel into their Vue 2 applications with a component-based API. The package is currently at version 1.2.0 and has been explicitly declared as no longer actively maintained by its primary contributor, indicating an abandoned status. It differentiates itself by offering a Vue-native syntax for controlling Slick, including prop-based options, method calls via refs, and event listeners. However, its core reliance on `slick-carousel` and `jQuery` introduces complexities, particularly concerning dependency management and potential version conflicts, which are highlighted in its documentation. Its support is strictly limited to Vue 2 environments.
Common errors
-
jQuery is not defined
cause The underlying slick-carousel library requires jQuery to be available globally, but it has not been loaded or correctly exposed in the application environment.fixInstall jQuery (`npm install jquery`) and ensure it's imported or globally exposed before slick-carousel and vue-slick are initialized. For webpack, you might need to use `new webpack.ProvidePlugin({ $: 'jquery', jQuery: 'jquery' })`. -
TypeError: this.$refs.slick.next is not a function
cause The Slick carousel instance or its methods are not available on the component's ref, often due to the carousel not being fully initialized or an incorrect ref name.fixVerify that the `Slick` component has `ref="slick"` and that `slick-carousel` and `jquery` are correctly loaded and initialized. Ensure you are calling methods within `this.$nextTick` if dealing with dynamic content changes that might cause re-initialization.
Warnings
- breaking The vue-slick package is explicitly no longer supported by its main contributor. This means no new features, bug fixes, or compatibility updates for future Vue or slick-carousel versions are expected. Users should consider migrating to actively maintained alternatives.
- breaking vue-slick is only compatible with Vue >= 2. It does not support Vue 3 or later versions due to breaking changes in Vue's API and component lifecycle.
- gotcha The underlying `slick-carousel` library, and by extension `vue-slick`, depends on jQuery. This can lead to issues with bundle size, potential conflicts if multiple jQuery versions are loaded, or difficulty integrating into projects that aim to avoid jQuery entirely.
Install
-
npm install vue-slick -
yarn add vue-slick -
pnpm add vue-slick
Imports
- Slick
const Slick = require('vue-slick');import Slick from 'vue-slick';
- Slick CSS
import 'vue-slick/dist/slick.css';
import 'slick-carousel/slick/slick.css';
Quickstart
import Vue from 'vue';
import Slick from 'vue-slick';
import 'slick-carousel/slick/slick.css';
new Vue({
el: '#app',
components: { Slick },
data() {
return {
slickOptions: {
slidesToShow: 3,
dots: true,
infinite: true,
speed: 500,
// Any other options that can be got from plugin documentation
},
slides: [
'http://placehold.it/2000x1000?text=Slide%201',
'http://placehold.it/2000x1000?text=Slide%202',
'http://placehold.it/2000x1000?text=Slide%203',
'http://placehold.it/2000x1000?text=Slide%204',
'http://placehold.it/2000x1000?text=Slide%205'
]
};
},
methods: {
next() {
this.$refs.slick.next();
},
prev() {
this.$refs.slick.prev();
},
reInit() {
// Helpful if you have to deal with v-for to update dynamic lists
this.$nextTick(() => {
this.$refs.slick.reSlick();
});
},
handleAfterChange(event, slick, currentSlide) {
console.log('After change:', currentSlide);
}
},
template: `
<div>
<button @click="prev">Previous</button>
<button @click="next">Next</button>
<button @click="reInit">Re-init</button>
<slick ref="slick" :options="slickOptions" @afterChange="handleAfterChange">
<a v-for="(src, index) in slides" :key="index" :href="src">
<img :src="src" alt="Slide Image" />
</a>
</slick>
</div>
`
});