Vue版本:3.0.2
刷机版本:6.3.5
我想在Vue3.0中使用Swiper的slideTo方法,但是好像不行。
你能告诉我如何使用它吗?https://codesandbox.io/s/interesting-hooks-1jblc?file=/src/App.vue
Vue版本:3.0.2
刷机版本:6.3.5
我想在Vue3.0中使用Swiper的slideTo方法,但是好像不行。
你能告诉我如何使用它吗?https://codesandbox.io/s/interesting-hooks-1jblc?file=/src/App.vue
您似乎正在使用一个库,但参考另一个库以获取文档。
您正在从 导入库swiper/vue
,因此它必须是官方的;在这种情况下,访问 Swiper 实例存在细微差别:您需要存储 Swiper 实例。(参见控制器模式)。所以在你的情况下:
<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>
不要与其他库 ( )的使用ref
混淆。vue-awesome-swiper