nuxt——swiper的使用

本文介绍了在Vue项目中使用Swiper组件遇到的两个主要问题及其解决方案。问题一是装包或引入时的版本冲突,通过降级Swiper版本可以解决。问题二是服务端渲染时`window`未定义,采用方案二的插件形式引入并配置解决。提供了详细的代码示例,包括废弃的页面内直接使用和推荐的插件方式,但注意不同版本的Swiper可能会影响轮播图的显示,需要试验找到适用的版本。
摘要由CSDN通过智能技术生成

装包

== bug1:装包报错,或者引入报错,是版本的为题,降低版本即可解决==
== bug2:页面刷新后报错:window在服务端不识别,所以使用后面的方案二==

"swiper": "^4.0.7",
"vue-awesome-swiper": "^3.1.3"

方案一:页面中使用(废弃)

<div>
		<swiper
          ref="mySwiper"
          :options="swiperOptions"
          style="width: 200px; height: 200px"
        >
          <swiper-slide
            v-for="item in 5"
            :key="item"
            style="height: 200px; background: blue"
          >
            11
          </swiper-slide>
          <div class="swiper-pagination" slot="pagination"></div>
        </swiper>
</div>

<script>
import "swiper/dist/css/swiper.css";
import { swiper, swiperSlide } from "vue-awesome-swiper";
export default {
  data() {
    return {
      swiperOptions: {
        loop: true,
        slidesPerView: 3, // 一屏3个
        centeredSlides: false, // 第一个默认居左
        spaceBetween: 70, // 间隔
        autoplay: true, // 自动播放
        grabCursor: true, // 鼠标放上去显示小手
        pagination: {  // 分页
          el: ".swiper-pagination", //与slot="pagination"处 class 一致
          clickable: true, //轮播按钮支持点击
        },
        on: {
          slideChange() {
            console.log("onSlideChangeEnd", this);
          },
          tap() {
            console.log("onTap", this);
          },
        },
      },
    };
  },
  components: {
    swiper,
    swiperSlide,
  },
};
</script>

方案二:使用插件的形式(推荐)

== bug:有的版本可能轮播图不会正常展示,需要试 ==

"swiper": "^5.4.5",
"vue-awesome-swiper": "^4.0.0",
"nuxt": "^2.15.8",
  • plugins/swiper.js
import Vue from 'vue'
import css from 'swiper/css/swiper.css' // 注意css路径
import VueAwesomeSwiper from 'vue-awesome-swiper'
export default () => {
Vue.use(VueAwesomeSwiper,{css})
}
  • nuxt.config.js
 plugins: [
    { src: "~/plugins/swiper.js", ssr: false }
  ],

  • components/swiperCpt.vue
<template>
  <div v-swiper:mySwiper="swiperOption" style="width: 100%">
    <div class="swiper-wrapper">
      <div class="swiper-slide" v-for="item in list" :key="item.id">
        <div>
          <img
            :src="item.fileId"
            class="w100 mb-20"
            style="height: 352px; border-radius: 8px"
            alt=""
          />
          <p class="size-24 line-1">{{ item.title }}</p>
          <p>
            {{ item.information }}
          </p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      swiperOption: {
        // loop: true,
        slidesPerView: 3,
        centeredSlides: false,
        spaceBetween: 70,
        autoplay: true,
        grabCursor: true,
      },
    };
  },
  props: ["list"],
  mounted() {},
};
</script>

  • 页面直接使用组件
<swiperCpt></swiperCpt>
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小曲曲

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值