1:安装 npm install swiper
2:引入组件和样式文件
<template>
<div class="swiper-container">
<div class="swiper-wrapper">
<!-- 添加轮播项 -->
<div class="swiper-slide">Slide 1</div>
<div class="swiper-slide">Slide 2</div>
<div class="swiper-slide">Slide 3</div>
</div>
<!-- 添加分页器 -->
<div class="swiper-pagination"></div>
</div>
</template>
<script>
import Swiper from 'swiper';
import 'swiper/css/swiper.css';
export default {
mounted() {
// 在组件挂载后初始化 Swiper
new Swiper('.swiper-container', {
// 配置选项
pagination: {
el: '.swiper-pagination',
},
});
},
};
</script>
<style scoped>
/* 组件内部的样式 */
.swiper-container {
width: 100%;
height: 300px;
}
.swiper-slide {
text-align: center;
line-height: 300px;
font-size: 24px;
background-color: #ccc;
}
</style>
swiper-bundle.min.css
是 Swiper 官方提供的打包版本的样式文件,包含了 Swiper 核心和默认主题的样式。引入: import 'swiper/swiper-bundle.min.css';import 'swiper/css/swiper.css';
引入默认样式