Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

injection "swiper" not found. With useSwiper() #7215

Open
5 of 6 tasks
Jonas949 opened this issue Nov 27, 2023 · 12 comments
Open
5 of 6 tasks

injection "swiper" not found. With useSwiper() #7215

Jonas949 opened this issue Nov 27, 2023 · 12 comments
Labels

Comments

@Jonas949
Copy link

Check that this is really a bug

  • I confirm

Reproduction link

https://stackblitz.com/edit/vitejs-vite-d4cq6o?file=src/App.vue

Bug description

I want to control the slide with a new button

so i use this hook useSwiper from
https://swiperjs.com/vue#use-swiper

the vue tip warn injection "swiper" not found.

1701081652636 1701081685774

Get undefined!

Expected Behavior

No response

Actual Behavior

No response

Swiper version

11.0.5

Platform/Target and Browser Versions

windows11 chrome

Validations

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • I'm willing to open a PR
@Jonas949 Jonas949 added the Vue label Nov 27, 2023
@OliveLeb
Copy link

OliveLeb commented Nov 29, 2023

I believe useSwiper can be used within a component which is inside the Swiper element slot

@songziman
Copy link

Depending on your needs, you need to write a button component and import userwiper within the component

@ZongYan30
Copy link

I meet the same problems.please tell me have you deal with it?

@Jonas949
Copy link
Author

I meet the same problems.please tell me have you deal with it?

Use ref

@baijunjie
Copy link

same problems

@Jeanp78
Copy link

Jeanp78 commented Jun 25, 2024

I made a new component with the button using the useSwiper() hook and used that component inside the Swiper component

It works for me

ButtonsComponent

SwiperComponent

Example.mp4

@Bukunmikuti
Copy link

Bukunmikuti commented Jun 27, 2024

All you have to do is to add the custom button (that uses swiper.slideNext) inside the component. You could either insert before the first or after the last swiper-slide component

@Jeanp78
Copy link

Jeanp78 commented Jun 27, 2024

All you have to do is to add the custom button (that uses swiper.slideNext) inside the component. You could either insert before the first or after the last swiper-slide component

It only works if you make a new component and there you use the useSwiper()

This works
it works

This does not works
it does not works

@evinguoxx
Copy link

component outside of swiper:
<swiper @init="(swiper)=>$swiper = swiper"> ... </swiper>
https://swiperjs.com/swiper-api#event-init
use init function save swiper,after you can use swiper api on you need

component inside of swiper:
https://swiperjs.com/vue#useswiper

@jxlagbh360
Copy link

QQ_1723182844762
QQ_1723182887910

QQ_1723182866632

you can use it as this to slove this problem.

@mzh0922
Copy link

mzh0922 commented Sep 3, 2024

I meet the same problems.please tell me have you deal with it?

Use ref

use ref , it dosen't work, please show the code

@mzh0922
Copy link

mzh0922 commented Sep 3, 2024

<template>
   <swiper  @afterInit="onAfterInit">
  </swiper>
</template>

<script lang="ts" setup>
import { Swiper, SwiperSlide } from 'swiper/vue'

const mySwiperRef: Ref<null|typeof Swiper> = ref(null)

const onAfterInit = (s: typeof Swiper) => {
  mySwiperRef.value = s
 }
</script>

you can get the intance of swiper by afterInit event

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

10 participants