[Vuejs]-Render function or template not defined in component: Swiper

5👍

The Swiper docs state:

Swiper Vue.js components are compatible only with new Vue.js version 3.

Nuxt 2.x currently uses Vue 2, which causes the error you mentioned. A workaround is to use Swiper’s API with HTML, as shown in the component below:

<template>
  <div class="swiper-container">
      <!-- Additional required wrapper -->
      <div class="swiper-wrapper">
          <!-- Slides -->
          <div class="swiper-slide">Slide 1</div>
          <div class="swiper-slide">Slide 2</div>
          <div class="swiper-slide">Slide 3</div>
          ...
      </div>
      <!-- If we need pagination -->
      <div class="swiper-pagination"></div>

      <!-- If we need navigation buttons -->
      <div class="swiper-button-prev"></div>
      <div class="swiper-button-next"></div>

      <!-- If we need scrollbar -->
      <div class="swiper-scrollbar"></div>
  </div>
</template>

<script>
import Swiper, { Navigation, Pagination } from 'swiper'
import 'swiper/swiper-bundle.css'

Swiper.use([ Navigation, Pagination ])

export default {
  mounted() {
    new Swiper('.swiper-container', {
      loop: true,
      pagination: {
        el: '.swiper-pagination',
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      },
      scrollbar: {
        el: '.swiper-scrollbar',
      },
    })
  }
}
</script>

<style scoped>
.swiper-container {
  width: 600px;
  height: 300px;
}
</style>
👤tony19

0👍

wrap the container in <client-only></client-only> if you are using newer version of nuxtjs. Use <no-ssr></no-ssr> for older versions

Leave a comment