vue中swiper?vue-awesome-swiper的使用方法及各種坑解決
一、什么是vue-awesome-swiper?
簡(jiǎn)而言之:
vue-awesome-swiper是基于swiper的Vue組件。是swiper推薦的在vue中使用swiper的方式。
vue-awesome-swiper的使用
1、安裝
npm install ?vue-awesome-swiper --save-dev
2.引用
? ? /*全局引入*/
? ? import VueAwesomeSwiper from 'vue-awesome-swiper'
? ? import 'swiper/dist/css/swiper.css'//這里注意具體看使用的版本是否需要引入樣式,以及具體位置。
? ? Vue.use(VueAwesomeSwiper, /* { default global options } */)
? ?
? ? /*組件方式引用*/
? ? import 'swiper/dist/css/swiper.css'這里注意具體看使用的版本是否需要引入樣式,以及具體位置。
? ? import { swiper, swiperSlide } from 'vue-awesome-swiper'
? ? export default {
? ? components: {
? ? ? swiper,
? ? ? swiperSlide
? ? }3.使用
就是一般組件的用法
? ? <swiper :options="swiperOption">
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? ? <swiper-slide><img src="static/images/jay.jpg"></swiper-slide>
? ? </swiper>
? ? <!--以下看需要添加-->
? ? <div class="swiper-scrollbar"></div> //滾動(dòng)條
? ? <div class="swiper-button-next"></div> //下一項(xiàng)
? ? <div class="swiper-button-prev"></div> //上一項(xiàng)
? ? <div class="swiper-pagination"></div> //標(biāo)頁碼
? data(){
? ? return{
? ? ? swiperOption: {//swiper3
? ? ? autoplay: 3000,
? ? ? speed: 1000,
? ? ? }
? ? }
? }二、由版本引起的一系列坑
坑1
按照上圖安裝方法,npm將安裝最新的vue-awesome-swiper(@4),對(duì)應(yīng)的是swiper6,但是國(guó)內(nèi)暫時(shí)沒有swiper6的文檔,意味著沒法參考使用方法,有問題也不好去網(wǎng)上找
坑2
最新版vue-awesome-swiper的安裝姿勢(shì)是這樣子滴:
npm install swiper vue-awesome-swiper --save
對(duì)比vue-awesome-swiper版本3
npm install vue-awesome-swiper --save-dev
坑3
這是網(wǎng)上大伙查找的最多的坑,搞了很久沒解決,有可能會(huì)導(dǎo)致小伙伴們暴躁易怒,哈哈
安裝完之后,你又在某度上查找使用方法,網(wǎng)友一般建議你這樣使用
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
export default {
? components: {
? ? swiper,
? ? swiperSlide
? },
? data() {
? ? return {
? ? ? swiperOption: {
? ? ? ? loop: true,
? ? ? ? autoplay: {
? ? ? ? ? delay: 3000,
? ? ? ? ? stopOnLastSlide: false,
? ? ? ? ? disableOnInteraction: false
? ? ? ? },
? ? ? ? // 顯示分頁
? ? ? ? pagination: {
? ? ? ? ? el: ".swiper-pagination",
? ? ? ? ? clickable: true //允許分頁點(diǎn)擊跳轉(zhuǎn)
? ? ? ? },
? ? ? ? // 設(shè)置點(diǎn)擊箭頭
? ? ? ? navigation: {
? ? ? ? ? nextEl: ".swiper-button-next",
? ? ? ? ? prevEl: ".swiper-button-prev"
? ? ? ? }
? ? ? }
? ? };
? },然后你的vue就使勁跟你報(bào)錯(cuò),說找不到swiper.css,然后你又繼續(xù)某度,無限坑。。。
或者你去看了一下路徑,再去node_modules找swiper,發(fā)現(xiàn)沒有swiper這貨。那就安裝個(gè)swiper唄
npm install swiper --save
但是,你沒有帶版本,npm默認(rèn)給你裝的是swiper6,文件夾里的路徑跟swiper4都不一樣啦兄弟們。
這才是問題的根源,加入你沒找到問題的核心,繼續(xù)某度的話,估計(jì)還沒好幾天辛苦滴爬坑。
正確的使用姿勢(shì):
安裝(指定版本)
npm install vue-awesome-swiper@3 --save-dev
組件中使用
這里我貼出在頁面中簡(jiǎn)單使用方法(先跑起來),小伙伴們可以完全復(fù)制粘貼,復(fù)雜的東西我都簡(jiǎn)化掉了。 版本: vue@2.5.2,vue-awesome-swiper@3.1.3
<template>
? <div class="recommendPage">
? ? <swiper :options="swiperOption" ref="mySwiper">
? ? ? <swiper-slide>I'm Slide 1</swiper-slide>
? ? ? <swiper-slide>I'm Slide 2</swiper-slide>
? ? ? <swiper-slide>I'm Slide 3</swiper-slide>
? ? ? <div class="swiper-pagination" slot="pagination"></div>
? ? ? <div class="swiper-button-prev" slot="button-prev"></div>
? ? ? <div class="swiper-button-next" slot="button-next"></div>
? ? </swiper>
? </div>
</template>
<script>
// 引入插件
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
export default {
? components: {
? ? swiper,
? ? swiperSlide
? },
? data() {
? ? return {
? ? ? swiperOption: {
? ? ? ? loop: true,
? ? ? ? autoplay: {
? ? ? ? ? delay: 3000,
? ? ? ? ? stopOnLastSlide: false,
? ? ? ? ? disableOnInteraction: false
? ? ? ? },
? ? ? ? // 顯示分頁
? ? ? ? pagination: {
? ? ? ? ? el: ".swiper-pagination",
? ? ? ? ? clickable: true //允許分頁點(diǎn)擊跳轉(zhuǎn)
? ? ? ? },
? ? ? ? // 設(shè)置點(diǎn)擊箭頭
? ? ? ? navigation: {
? ? ? ? ? nextEl: ".swiper-button-next",
? ? ? ? ? prevEl: ".swiper-button-prev"
? ? ? ? }
? ? ? }
? ? };
? },
? computed: {
? ? swiper() {
? ? ? return this.$refs.mySwiper.swiper;
? ? }
? },
? mounted() {
? ? // current swiper instance
? ? // 然后你就可以使用當(dāng)前上下文內(nèi)的swiper對(duì)象去做你想做的事了
? ? console.log("this is current swiper instance object", this.swiper);
? ? // this.swiper.slideTo(3, 1000, false);
? }
};
</script>
<style scoped >
.recommendPage .swiper-container{
? position: relative;
? width: 100%;
? height: 200px;
? background: pink;
} ?
.recommendPage .swiper-container .swiper-slide{
? width: 100%;
? line-height: 200px;
? background: yellowgreen;
? color: #000;
? font-size: 16px;
? text-align: center;
}
</style>三、例子
我寫的是局部的,只需要在 在ha.vue頁面 寫好如下結(jié)構(gòu)
<template> ?? ?<div class=''> ? ?<swiper :options="swiperOption" ref="mySwiper"> ? ? ? <swiper-slide v-for="item in slide" :key="item.imgUrl"> ? ? ? ? <img :src="item.imgUrl" alt="" class="swiper-slide-img" width="100%" height="100%" ? ? ? /></swiper-slide> ? ? ? <div class="swiper-pagination" slot="pagination"></div> ? ? </swiper> ? </div> </template>
在script引入
import { swiper, swiperSlide } from "vue-awesome-swiper";
import "swiper/dist/css/swiper.css";
export default {
? components: {
? ? swiper,
? ? swiperSlide
? },
? data() {
? ? return {
? ? ? swiperOption: {
? ? ? ? loop: true,
? ? ? ? autoplay: {
? ? ? ? ? delay: 3000,
? ? ? ? ? stopOnLastSlide: false,
? ? ? ? ? disableOnInteraction: false
? ? ? ? },
? ? ? ? // 顯示分頁
? ? ? ? pagination: {
? ? ? ? ? el: ".swiper-pagination",
? ? ? ? ? clickable: true //允許分頁點(diǎn)擊跳轉(zhuǎn)
? ? ? ? },
? ? ? ? // 設(shè)置點(diǎn)擊箭頭
? ? ? ? navigation: {
? ? ? ? ? nextEl: ".swiper-button-next",
? ? ? ? ? prevEl: ".swiper-button-prev"
? ? ? ? }
? ? ? }
? ? };
? },
? computed: {
? ? swiper() {
? ? ? return this.$refs.mySwiper.swiper;
? ? }
? },
? mounted() {
? ? // current swiper instance
? ? // 然后你就可以使用當(dāng)前上下文內(nèi)的swiper對(duì)象去做你想做的事了
? ? console.log("this is current swiper instance object", this.swiper);
? ? // this.swiper.slideTo(3, 1000, false);
? }
};
</script>
<style scoped >
</style>還有一些關(guān)于版本的坑,反正真的很坑,不然我不會(huì)大半夜氣呼呼在這寫這玩意
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Vue學(xué)習(xí)-VueRouter路由基礎(chǔ)
這篇文章主要介紹了Vue學(xué)習(xí)-VueRouter路由基礎(chǔ),路由本質(zhì)上就是超鏈接,xiamian?文章圍繞VueRouter路由的相關(guān)資料展開詳細(xì)內(nèi)容,需要的小伙伴可以參考一下,希望對(duì)你的學(xué)習(xí)有所幫助2021-12-12
element中el-cascader級(jí)聯(lián)選擇器只有最后一級(jí)可以多選
本文主要介紹了element中el-cascader級(jí)聯(lián)選擇器只有最后一級(jí)可以多選,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-01-01
vue項(xiàng)目啟動(dòng)如何設(shè)置默認(rèn)啟動(dòng)頁
這篇文章主要介紹了vue項(xiàng)目啟動(dòng)如何設(shè)置默認(rèn)啟動(dòng)頁問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
vue?element-ui?Radio單選框默認(rèn)值選不中的原因:混用字符和數(shù)字問題
這篇文章主要介紹了vue?element-ui?Radio單選框默認(rèn)值選不中的原因:混用字符和數(shù)字問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
vue3中element-plus?Upload上傳文件代碼示例
這篇文章主要介紹了vue3中element-plus?Upload上傳文件的相關(guān)資料,在時(shí)間開發(fā)中上傳文件是經(jīng)常遇到的一個(gè)需求,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
使用vue3實(shí)現(xiàn)一個(gè)人喵交流小程序
Vue3 在經(jīng)過多個(gè)開發(fā)版本的迭代后,終于迎來了它的正式版本,下面這篇文章主要給大家介紹了關(guān)于如何使用vue3實(shí)現(xiàn)一個(gè)人喵交流小程序的相關(guān)資料,需要的朋友可以參考下2021-11-11
VUE2.0+ElementUI2.0表格el-table循環(huán)動(dòng)態(tài)列渲染的寫法詳解
這篇文章主要介紹了VUE2.0+ElementUI2.0表格el-table循環(huán)動(dòng)態(tài)列渲染的寫法詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11

