可以使用参考来选择swiper方法

发布于 2025-02-11 07:10:27 字数 1816 浏览 1 评论 0原文

我正在尝试调用.slidenext()&当用户按下箭头键时,swiper随附的slideprev。

我设法使用像这样的Querylesector来完成此操作:

const changeSlide = (event: KeyboardEvent) => {

    const slides = document.querySelector('.swiper').swiper

    if(event.key === 'ArrowLeft') {
      slides.slidePrev();
    } else if (event.key === 'ArrowRight') {
      slides.slideNext();
    }

}

但是,此方法通常不允许使用该项目来产生警告,并且该项目的使用不允许使用。因此,相反,我不是要使用ref选择刀片,但我还没有成功。

这就是我尝试的:

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { Navigation } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/navigation';
// just some imports that are needed

const swiperRef = ref();
// swiper as ref

const changeSlide = (event: KeyboardEvent) => {

    const slides = swiperRef.value.swiper
    // instead of querySelector i use the ref.value

    if(event.key === 'ArrowLeft') {
      slides.slidePrev();
    } else if (event.key === 'ArrowRight') {
      slides.slideNext();
    }

}
</script>

<template>
<swiper
  ref="swiperRef"
  :initial-slide="props.initialSlide"
  :slides-per-view="1"
  :space-between="0"
  :modules="[Navigation]"
  navigation
>
  <swiper-slide>  // left out the attributes since they are a mere distraction
    <img/> // left out the attributes since they are a mere distraction
  </swiper-slide>
</swiper>
</template>

我从此代码中获得的错误是:

I am trying to invoke .slideNext() & .slidePrev that come with swiper when the user presses the arrow keys.

I've managed to do this with querySelector like this:

const changeSlide = (event: KeyboardEvent) => {

    const slides = document.querySelector('.swiper').swiper

    if(event.key === 'ArrowLeft') {
      slides.slidePrev();
    } else if (event.key === 'ArrowRight') {
      slides.slideNext();
    }

}

However this method creates warnings and the usage of querySelector is not allowed in general for this project. So instead i wan't to use a ref to select the swiper but i've not yet succeeded at this.

This is what i tried:

<script setup lang="ts">
import { ref, onMounted, onUnmounted } from 'vue';
import { Navigation } from 'swiper';
import { Swiper, SwiperSlide } from 'swiper/vue';
import 'swiper/css';
import 'swiper/css/navigation';
// just some imports that are needed

const swiperRef = ref();
// swiper as ref

const changeSlide = (event: KeyboardEvent) => {

    const slides = swiperRef.value.swiper
    // instead of querySelector i use the ref.value

    if(event.key === 'ArrowLeft') {
      slides.slidePrev();
    } else if (event.key === 'ArrowRight') {
      slides.slideNext();
    }

}
</script>

<template>
<swiper
  ref="swiperRef"
  :initial-slide="props.initialSlide"
  :slides-per-view="1"
  :space-between="0"
  :modules="[Navigation]"
  navigation
>
  <swiper-slide>  // left out the attributes since they are a mere distraction
    <img/> // left out the attributes since they are a mere distraction
  </swiper-slide>
</swiper>
</template>

The error i get from this code is:
enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南七夏 2025-02-18 07:10:27

从我在,您无法使用refSwiper vue组件上使用swiper实例,因为它没有公开。

您有其他思考的方式:

  • &lt; swiper&gt;组件的子组件内,使用usewiper() component。
  • 在将&lt; swiper&gt;组件构成的父组件上,使用@swiper事件。它在安装后发送swiper对象:
<template>
 <swiper @swiper="getRef" />
</template>

<script setup>
const swiper = ref(null)
function getRef (swiperInstance) {
  swiper.value = swiperInstance
}

function next () {
  swiper.value.slideNext() // should work
}
</script>

From what I'm seeing in the source code, you can't access the swiper instance using ref on the swiper vue component because it's not exposed.

You have others way to do thought:

  • inside a child component of the <swiper> component, use the useSwiper() component.
  • on the parent component that instanciate the <swiper> component, use the @swiper event. It sends the swiper object once mounted:
<template>
 <swiper @swiper="getRef" />
</template>

<script setup>
const swiper = ref(null)
function getRef (swiperInstance) {
  swiper.value = swiperInstance
}

function next () {
  swiper.value.slideNext() // should work
}
</script>
扶醉桌前 2025-02-18 07:10:27

您不必使用此方法,而可以像这样导入键盘

import { Navigation } from 'swiper';

将键盘添加到模块和&lt; swiper&gt;的属性:

<swiper :modules="[Navigation, Keyboard]" keyboard />

并将属性添加到&lt; swiper-slide&gt;

<swiper-slide keyboard />

You don't have to use this method instead you can just import Keyboard like this:

import { Navigation } from 'swiper';

Add Keyboard to modules and attributes of <swiper> like this:

<swiper :modules="[Navigation, Keyboard]" keyboard />

And add the attribute to <swiper-slide> as well:

<swiper-slide keyboard />

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文