Home > Web Front-end > Vue.js > body text

How to use swiper plug-in in vue

下次还敢
Release: 2024-05-09 15:33:20
Original
1161 people have browsed it

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
vue
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template