请安装swiper:5.3.6版本,目前6.0版本使用有问题。
插件安装:
cnpm install swiper@^5.3.6 vue-awesome-swiper@4.0.4 --save
或者使用yarn add swiper@^5.3.6 vue-awesome-swiper@4.0.4 --save
需要同时安装swiper和vue-awesome-swiper两个插件。
用法:
import { Swiper, SwiperSlide, directive } from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'
export default {
components: { Swiper, SwiperSlide }
}
如果报错的话,可能需要添加一个指令:
export default {
components: { Swiper, SwiperSlide },
directives: {
swiper: directive
}
}
Vue3大家可以采用这种写法:https://swiperjs.com/vue
npm i swiper 或者 yarn add swiper
<template>
<swiper
:slides-per-view="3"
:space-between="50"
@swiper="onSwiper"
@slideChange="onSlideChange"
>
<swiper-slide>Slide 1</swiper-slide>
<swiper-slide>Slide 2</swiper-slide>
<swiper-slide>Slide 3</swiper-slide>
... </swiper></template><script>
// Import Swiper Vue.js components
import { Swiper, SwiperSlide } from 'swiper/vue';
// Import Swiper styles
import 'swiper/css';
export default {
components: {
Swiper,
SwiperSlide,
},
setup() {
const onSwiper = (swiper) => {
console.log(swiper);
};
const onSlideChange = () => {
console.log('slide change');
};
return {
onSwiper,
onSlideChange,
};
},
};</script>