首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Vue3.0中使用Swiper ()?

如何在Vue3.0中使用Swiper ()?
EN

Stack Overflow用户
提问于 2020-11-17 09:49:21
回答 1查看 4.9K关注 0票数 1

Vue版本:3.0.2

刷卡版本:6.3.5

我想在Vue3.0中使用Swiper的slideTo方法,但它似乎不起作用。

你能告诉我怎么用吗?https://codesandbox.io/s/interesting-hooks-1jblc?file=/src/App.vue

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-11-17 12:22:52

您似乎在使用一个库,但在文档中引用另一个库。

您正在从swiper/vue导入库,因此它必须是正式库;在这种情况下,在访问Swiper实例方面有一个细微的差别:您需要存储Swiper实例。(见控制器模式)。所以在你的情况下:

代码语言:javascript
复制
<template>
  <swiper
    style="border: 1px solid red; width: 400px"
    :slides-per-view="1"
    @swiper="onSwiper"
  >
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    <swiper-slide>Slide 4</swiper-slide>
  </swiper>

  <button @click="handleSlideTo">slide to 4</button>
</template>

<script>
import { Swiper, SwiperSlide } from "swiper/vue";
import "swiper/swiper-bundle.css";

export default {
  name: "App",
  
  components: {
    Swiper,
    SwiperSlide,
  },

  data() {
    return {
      swiper: null,
    };
  },

  methods: {
    onSwiper(swiper) {
      this.swiper = swiper;
    },

    handleSlideTo() {
      this.swiper.slideTo(3);
    },
  },
};
</script>

不要被其他库(vue-awesome-swiper)与vue-awesome-swiper混淆。

票数 13
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64872840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档