Home >Web Front-end >Vue.js >How to use swiper plug-in in vue

How to use swiper plug-in in vue

下次还敢
下次还敢Original
2024-05-09 15:33:201231browse

How to use the Swiper plug-in in Vue: Install the Swiper plug-in: npm install --save swiper Import the Swiper plug-in and install Vue.use(Swiper) Create Swiper components and create Swiper instances Configure Swiper options, such as automatic play, Loop playback and paging destroy the Swiper instance when the component is destroyed

How to use swiper plug-in in vue

How to use the Swiper plug-in in Vue

Install the Swiper plug-in

<code class="bash">npm install --save swiper</code>

Introduce Swiper plug-in

Introduce Swiper into the Vue main file:

<code class="javascript">// main.js
import Vue from 'vue'
import Swiper from 'swiper'
Vue.use(Swiper)</code>

Create Swiper component

Create a Swiper instance in the Vue component:

<code class="html"><template>
  <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-pagination"></div>
  </div>
</template>

<script>
import { Swiper } from 'swiper'
export default {
  mounted() {
    // 创建 Swiper 实例
    new Swiper('.swiper-container', {
      // 设置 Swiper 选项
      pagination: {
        el: '.swiper-pagination'
      }
    })
  }
}
</script></code>

Configure Swiper options

Use the Swiper option object to configure Swiper:

<code class="javascript">new Swiper('.swiper-container', {
  // 设置自动播放
  autoplay: {
    delay: 2000
  },
  // 启用循环播放
  loop: true,
  // 启用分页
  pagination: {
    el: '.swiper-pagination'
  }
})</code>

Destroy the Swiper instance

Destroy the Swiper instance when the component is destroyed:

<code class="javascript">export default {
  mounted() {
    // 创建 Swiper 实例
    this.swiperInstance = new Swiper('.swiper-container', {
      // 设置 Swiper 选项
    })
  },
  beforeDestroy() {
    // 销毁 Swiper 实例
    this.swiperInstance.destroy()
  }
}</code>

The above is the detailed content of How to use swiper plug-in in vue. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn