vue2 使用 Swiper vue-awesome-swiper 实现轮播效果

使用vue2以及swiper5.4.5 vue-awesome-swiper实现轮播效果

首先使用命令行

        1安装   

cnpm install swiper@5.4.5 vue-awesome-swiper --save

swiper一定要是5.4.5版本,安装完成后

main.js中引入做全局注册

import Vue from 'vue'
import App from './App.vue'
import VueAwesomeSwiper from 'vue-awesome-swiper'
import 'swiper/css/swiper.css'

Vue.config.productionTip = false
Vue.use(VueAwesomeSwiper)
new Vue({
  render: h => h(App),
}).$mount('#app')

在自己创建的组件中    .vue文件中写入

<template>
  <swiper ref="mySwiper" :options="swiperOptions">
    <swiper-slide>Slide 1</swiper-slide>
    <swiper-slide>Slide 2</swiper-slide>
    <swiper-slide>Slide 3</swiper-slide>
    <swiper-slide>Slide 4</swiper-slide>
    <swiper-slide>Slide 5</swiper-slide>
    <div class="swiper-pagination" slot="pagination"></div>
  </swiper>
</template>
 
<script>
  export default {
    name: 'HelloWorld',
    data() {
      return {
        swiperOptions: {
          pagination: {
            el: '.swiper-pagination'
          },
          // Some Swiper option/callback...
        }
      }
    },
    computed: {
      swiper() {
        return this.$refs.mySwiper.$swiper
      }
    },
    mounted() {
      console.log('Current Swiper instance object', this.swiper)
      this.swiper.slideTo(3, 1000, false)
    }
  }
</script> 

自己需要的样式在此段代码后添加一个<style></style>标签,添加样式。

在APP.vue中引用

<template>
  <div id="app">
//引用组件
    <HelloWorld/>
  </div>
</template>

<script>
//引入组件
import HelloWorld from './components/HelloWorld.vue'

export default {
  name: 'App',
  components: {
//组件名称
    HelloWorld
  }
}
</script>

这样运行就可以看到轮播了。


版权声明:本文为weixin_60570544原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。