欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue中引用swiper輪播插件的教程詳解

 更新時(shí)間:2018年08月16日 11:36:09   作者:做一個(gè)對社會有用的社會青年......  
這篇文章主要介紹了vue中引用swiper輪播插件的方法,在需要使用swiper的組件里引入swiper,swiper的初始化放在mounted里。具體實(shí)例代碼大家跟隨腳本之家小編一起看看吧

有時(shí)候我們需要在vue中使用輪播組件,如果是在vue組件中引入第三方組件的話,最好通過npm安裝,從而進(jìn)行統(tǒng)一安裝包管理。

申明:本文所使用的是vue.2x版本。

通過npm安裝插件:

 npm install swiper --save-dev

在需要使用swiper的組件里引入swiper,swiper的初始化放在mounted里

Slider.vue源碼:

<template>
 <div class="swiper-container">
 <div class="swiper-wrapper">
 <div class="swiper-slide"><img src="../fixtures/sliders/t1.svg"/></div>
 <div class="swiper-slide"><img src="../fixtures/sliders/t2.svg"/></div>
 <div class="swiper-slide">Slide 3</div>
 </div>
 <!-- 如果需要分頁器 -->
 <div class="swiper-pagination"></div>
 <!-- 如果需要導(dǎo)航按鈕 -->
 <!--<div class="swiper-button-prev"></div>-->
 <!--<div class="swiper-button-next"></div>-->
 <!-- 如果需要滾動條 -->
 <!--<div class="swiper-scrollbar"></div>-->
 </div>
</template>
<script>
 import 'swiper/dist/css/swiper.css'
 import Swiper from 'swiper';
 export default {
 name: "Slider",
 mounted(){
 new Swiper ('.swiper-container', {
 loop: true,
 // 如果需要分頁器
 pagination: '.swiper-pagination',
 // 如果需要前進(jìn)后退按鈕
 nextButton: '.swiper-button-next',
 prevButton: '.swiper-button-prev',
 // 如果需要滾動條
 scrollbar: '.swiper-scrollbar',
 })
 }
 }
</script>
<style scoped>
 .swiper-container {
 width: 100%;
 margin: 0;
 padding: 0;
 }
 .swiper-wrapper {
 height: 200px;
 }
 .swiper-slide img {
 max-width: 100%;
 }
 .swiper-slide {
 text-align: center;
 background: #fff;
 /* Center slide text vertically */
 display: -webkit-box;
 display: -ms-flexbox;
 display: -webkit-flex;
 display: flex;
 -webkit-box-pack: center;
 -ms-flex-pack: center;
 -webkit-justify-content: center;
 justify-content: center;
 -webkit-box-align: center;
 -ms-flex-align: center;
 -webkit-align-items: center;
 align-items: center;
 }
</style>

運(yùn)行效果:

接下來,我們對上面的代碼進(jìn)行重構(gòu),因?yàn)槿绻覀冇?css 選擇器作為 Swiper 定位頁面上元素依據(jù)的話,假如在一個(gè)頁面上同時(shí)有兩個(gè).slider-container,那么這個(gè)組件就會亂套 !我們要秉承著低耦合的開發(fā)方式來重構(gòu)我們的代碼。

我們可以使用Vue提供的更精確的指明方式在元素中添加ref熟悉,然后在代碼內(nèi)通過 this.$refs.引用名來引用。

這是Vue.js2.0后的編號,ref標(biāo)記是標(biāo)準(zhǔn)的HTML屬性,它取代了Vue.js 1.x中v-ref的寫法

需要注意的是,如果改為動態(tài)綁定圖片,請參考:vue-cil和webpack中本地靜態(tài)圖片的路徑問題解決方案

我這里將靜態(tài)資源文件轉(zhuǎn)移到了static目錄下面。

重構(gòu)后的代碼如下:

<template>
 <div>
 <div class="swiper-container" ref="slider">
 <div class="swiper-wrapper">
 <div class="swiper-slide" v-for="slide in slides">
 <router-link :to="{name:'BookDetail',params:{id:slide.id}}">
 <img :src="slide.img_url"/>
 </router-link>
 </div>
 </div>
 </div>
 </div>
</template>
<script>
 import 'swiper/dist/css/swiper.css'
 import Swiper from 'swiper'
 export default {
 name: "Slider",
 data(){
 return{
 slides:[{id:1,img_url:'./static/sliders/t1.svg'},{id:2,img_url:'./static/sliders/t2.svg'}]
 }
 },
 mounted(){
 new Swiper (this.$refs.slider, {
 loop: true,
 // 如果需要分頁器
 pagination: '.swiper-pagination',
 // 如果需要前進(jìn)后退按鈕
 nextButton: '.swiper-button-next',
 prevButton: '.swiper-button-prev',
 // 如果需要滾動條
 scrollbar: '.swiper-scrollbar',
 })
 }
 }
</script>

這里還沒有把組件完全獨(dú)立,里面有數(shù)據(jù)定義,其實(shí)可以把這個(gè)數(shù)據(jù)作為一個(gè)參數(shù)傳遞進(jìn)來,也就是組件之間數(shù)據(jù)傳遞。

Vue頁面跳轉(zhuǎn)傳參

通過路由傳參,在router/index.js中定義路由

export default new Router({
 routes: [
 {
 name:'BookDetail',
 path:'/books/:id',
 component: BookDetail
 }
 ]
})

前面的輪播組件中已經(jīng)定義了需要傳遞的路由參數(shù)

 <router-link :to="{name:'BookDetail',params:{id:slide.id}}">
 <img :src="slide.img_url"/>
 </router-link>

參數(shù)接收界面BookDetail.vue

<template>
<div>
 點(diǎn)擊的是:<span v-text="id"></span>
</div>
</template>
<script>
 export default {
 name: "BookDetail",
 data(){
 return{
  id:this.$route.params.id
 }
 },
 props:[]
 }
</script>
<style scoped>
</style>

如果傳遞參數(shù)太多,這樣的方式肯定不方便,那么可以采用vuex,或者組件數(shù)據(jù)傳遞。

關(guān)于組件傳值可以參考:Vue 組件之間傳值

總結(jié)

以上所述是小編給大家介紹的vue中引用swiper輪播插件的教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評論