Swiper/vue useSwiper() doesn’t work, empty value undefined.

huangapple go评论64阅读模式
英文:

Swiper/vue useSwiper() doesn't work, empty value undefined

问题

我已创建了我的导航,并希望使用useSwiper方法来控制按钮,但该方法为空。在沙箱中,我创建了一个完全重复我的代码的示例。正常导航可以工作,但当我想要使用我的按钮时,出现错误。 Sandbox

Swiper/vue useSwiper() doesn’t work, empty value undefined.

英文:

I have created my navigation and want to use the useSwiper method to control the buttons, but the method is empty. In the sandbox, I created an example that completely repeats my code. Normal navigation works, but when I want to use my buttons I get an error. Sandbox

Swiper/vue useSwiper() doesn’t work, empty value undefined.

答案1

得分: 0

useSwiper 钩子必须在位于 Swiper 内部的组件中使用。在你的情况下,swiperundefined

你可以从 onSwiper 事件中获取 swiper。 你可以在 codesandbox 中获取更多信息:

<template>
  <Swiper
    @swiper="onSwiper"
    :navigation="true"
    :modules="modules"
    class="mySwiper"
  >
    <div>
      <button @click.stop="slider.slidePrev()">prev</button>
      <button @click.stop="slider.slideNext()">next</button>
    </div>

    <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-slide>Slide 5</swiper-slide>
    <swiper-slide>Slide 6</swiper-slide><swiper-slide>Slide 7</swiper-slide>
    <swiper-slide>Slide 8</swiper-slide><swiper-slide>Slide 9</swiper-slide>
  </Swiper>
</template>
<script setup>
import { ref } from "vue";
import "swiper/swiper-bundle.min.css";
import { Navigation } from "swiper";
import { Swiper, SwiperSlide } from "swiper/vue";

const modules = [Navigation];
const slider = ref(null);
const onSwiper = (swiper) => {
  slider.value = swiper;
};
</script>
英文:

useSwiper hook must be used in components which are inside of the Swiper. In your case swiper is undefined.

You can get swiper from onSwiper event codesandbox:

&lt;template&gt;
  &lt;Swiper
    @swiper=&quot;onSwiper&quot;
    :navigation=&quot;true&quot;
    :modules=&quot;modules&quot;
    class=&quot;mySwiper&quot;
  &gt;
    &lt;div&gt;
      &lt;button @click.stop=&quot;slider.slidePrev()&quot;&gt;prev&lt;/button&gt;
      &lt;button @click.stop=&quot;slider.slideNext()&quot;&gt;next&lt;/button&gt;
    &lt;/div&gt;

    &lt;Swiper-slide&gt;Slide 1&lt;/Swiper-slide&gt;
    &lt;swiper-slide&gt;Slide 2&lt;/swiper-slide&gt;&lt;swiper-slide&gt;Slide 3&lt;/swiper-slide&gt;
    &lt;swiper-slide&gt;Slide 4&lt;/swiper-slide&gt;&lt;swiper-slide&gt;Slide 5&lt;/swiper-slide&gt;
    &lt;swiper-slide&gt;Slide 6&lt;/swiper-slide&gt;&lt;swiper-slide&gt;Slide 7&lt;/swiper-slide&gt;
    &lt;swiper-slide&gt;Slide 8&lt;/swiper-slide&gt;&lt;swiper-slide&gt;Slide 9&lt;/swiper-slide&gt;
  &lt;/Swiper&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from &quot;vue&quot;;
import &quot;swiper/swiper-bundle.min.css&quot;;
import { Navigation } from &quot;swiper&quot;;
import { Swiper, SwiperSlide } from &quot;swiper/vue&quot;;

const modules = [Navigation];
const slider = ref(null);
const onSwiper = (swiper) =&gt; {
  slider.value = swiper;
};
&lt;/script&gt;

huangapple
  • 本文由 发表于 2023年4月6日 21:06:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75949883.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定