Swiper initializes the first time to get the element undefined

it makes sense to initialize the callback init to indicate that swiper should be loaded, but what you get is undefined , and it"s normal to add a timer. What"s the problem?

html

<div class="swiper-container">
    <div class="swiper-wrapper">
        <div class="swiper-slide">Slide 1</div>
        <div class="swiper-slide">Slide 2</div>
        <div class="swiper-slide">Slide 3</div>
    </div>

    <!--  -->
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>

</div>

JavaScript

window.onload = () => {
    new Swiper(".swiper-container", {
        // loop: true,
        // 
        navigation: {
            nextEl: ".swiper-button-next",
            prevEl: ".swiper-button-prev",
        },
        on: {
            init() {
                console.log("")
                console.log(this.navigation.nextEl) //undefined

                setTimeout(() => {
                    console.log(this.navigation.nextEl); //object
                }, 10)
            }
        }
    })
}

clipboard.png

undefined

init demo

clipboard.png

Jun.25,2021

Hello friend, it is officially recommended to call mySwiper.navigation.$prevEl

Menu