<script lang="ts" setup>
import { Swiper, SwiperSlide } from "swiper/vue";
import { ref } from "vue";
import { getCompanyNameList } from "@/api/auth";
import { ElMessage } from "element-plus";
import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/navigation";
import "swiper/css/scrollbar";
import { Autoplay, Pagination, Navigation, Scrollbar } from "swiper";
const modules = [Autoplay, Pagination, Navigation];
const bgColor = ref([
"background-color: #fff9c4",
"background-color: #b3e5fc",
"background-color: #ffebee",
"background-color: #bfc",
"background-color: skyblue",
]);
const listData = ref([]);
const getDetailsList = async () => {
const { data } = await getCompanyNameList();
if (data.length > 0) {
data.forEach((element: { bgColor: string }, i: number) => {
element.bgColor = bgColor.value[i];
});
listData.value = data;
} else {
ElMessage({
message: "暂无数据",
type: "warning",
});
}
};
getDetailsList();
</script>
```html
<template>
<div class="home">
<!-- :loop="true" -->
<!-- :scrollbar="{ draggable: true }" -->
<swiper
:modules="modules"
:slides-per-view="5"
:direction="'vertical'"
:autoplay="{ delay: 3000, disableOnInteraction: false }"
:pagination="{ clickable: true }"
>
<!-- loop可循环轮播,autoplay可自动播放 -->
<swiper-slide v-for="item in listData" :style="item.bgColor">{{
item.companyname
}}</swiper-slide>
</swiper>
</div>
</template>
最后别忘了 npm install swiper 安装一下依赖