Vue Swiper組件實(shí)現(xiàn)記錄
Swiper組件
下載swiper
首先下載swiper組件
注意與vue版本相符的swiper,因?yàn)槭莢ue版本為2,故下載swiper@5
不指定版本號的話,會下載最新的版本,可能會有適配問題。
在命令行輸入
npm i --save swiper@5
或者
npm install swiper@5
因?yàn)槲业南螺d時(shí),提示有依賴問題,所以是使用以下命令:
npm install swiper@5 --legacy-peer-deps
安裝成功界面:
創(chuàng)建swiper組件
views里放頁面組件;mycomponents中放公共組件或者子組件。
在src/mycomponents/films文件夾下創(chuàng)建FilmSwiper.vue組件。
創(chuàng)建后目錄如下:
保存時(shí)修復(fù)
徹底關(guān)閉規(guī)則檢查 no-new
自動(dòng)控制eslint檢查提醒 最后保存時(shí)再修復(fù)。
在.eslint.js中設(shè)置如下:
編寫swiper內(nèi)容
把原來的swiper組件 按照單文件組件創(chuàng)建 組件寫入
但是需要修改swiper的new方式。
示例如下:
<template> <div class="swiper-container demo"> <div class="swiper-wrapper"> <slot></slot> </div> <!-- 如果需要分頁器 --> <div class="swiper-pagination"></div> </div> </template> <script> export default { props: { loop: { type: Boolean, default: true } }, mounted () { new Swiper('.demo', { // 如果需要分頁器 pagination: { el: '.swiper-pagination' }, loop: this.loop, autoplay: { delay: 2500, disableOnInteraction: false } }) } } </script>
引入swiper
在films/FilmSwiper.vue中引入swiper。
示例如下:
使用swiper
在FilmsView.vue頁面中使用swiper組件;首先需要引入FilmSwiper組件。
示例如下:
<template> <div> <film-swiper> <div class="swiper-slide">11111111</div> <div class="swiper-slide">22222222</div> <div class="swiper-slide">33333333</div> </film-swiper> <div> 二級的聲明式導(dǎo)航 </div> <router-view></router-view> </div> </template> <script> import filmSwiper from '@/mycomponents/films/FilmSwiper' export default { components: { filmSwiper } } </script>
Swiper子組件
創(chuàng)建Swiper列表組件
修改filmsView.vue頁面中列表內(nèi)容封裝為組件。
也就是以下圈著的部分,把這塊封裝為組件:
在mycomponents/films下創(chuàng)建FilmSwiperItem.vue。
內(nèi)容如下:
<template> <div class="swiper-slide"> <slot></slot> </div> </template>
使用子組件
導(dǎo)入到FilmsView.vue中,然后注冊組件,最后使用組件替換原來的div列表。
示例如下:
<template> <div> <film-swiper> <film-swiper-item>11111111</film-swiper-item> <film-swiper-item>22222222</film-swiper-item> <film-swiper-item>33333333</film-swiper-item> </film-swiper> <div> 二級的聲明式導(dǎo)航 </div> <router-view></router-view> </div> </template> <script> import filmSwiper from '@/mycomponents/films/FilmSwiper' import filmSwiperItem from '@/mycomponents/films/FilmSwiperItem' export default { components: { filmSwiper, filmSwiperItem } } </script>
增加生命周期
改為從生命周期中加載列表數(shù)據(jù),然后渲染列表。
設(shè)置data和生命周期mounted函數(shù)。
示例如下:
<template> <div> <!-- 增加判斷 數(shù)據(jù)加載完成后渲染 --> <film-swiper :key="datalist.length"> <film-swiper-item v-for="data in datalist" :key="data">{{data}}</film-swiper-item> </film-swiper> <div> 二級的聲明式導(dǎo)航 </div> <router-view></router-view> </div> </template> <script> import filmSwiper from '@/mycomponents/films/FilmSwiper' import filmSwiperItem from '@/mycomponents/films/FilmSwiperItem' export default { data () { return { datalist: [] } }, mounted () { // 模擬獲取后端數(shù)據(jù) setTimeout(() => { this.datalist = ['11111111', '22222222', '33333333'] }, 1000) }, components: { filmSwiper, filmSwiperItem } } </script>
增加圖片顯示
整理banner數(shù)據(jù)到banner.json文件中,放入到public下:
加載數(shù)據(jù)
在生命周期mounted函數(shù)中,修改為使用axios請求本地banner.json文件中的數(shù)據(jù)。
并賦值到datalist中。
示例如下:
export default { data () { return { datalist: [] } }, mounted () { // 模擬獲取后端數(shù)據(jù) // setTimeout(() => { // this.datalist = ['11111111', '22222222', '33333333'] // }, 1000) axios.get('/banner.json').then(res => { console.log(res.data.data) this.datalist = res.data.data }) },
渲染修改
渲染列表時(shí)修改key綁定字段和圖片渲染字段。
示例如下:
<film-swiper :key="datalist.length"> <film-swiper-item v-for="data in datalist" :key="data.book_id"> <img :src="data.cover" alt=""> </film-swiper-item> </film-swiper>
圖片鏈接處理
圖片鏈接使用的是相對路徑,通過過濾器對圖片鏈接進(jìn)行拼接域名,
示例如下:
import Vue from 'vue' // 使用過濾器 處理圖片鏈接 Vue.filter('getImg', (path) => { return 'https://www.matiji.net' + path })
圖片樣式設(shè)置
在圖片的父級上增加類設(shè)置并對圖片進(jìn)行響應(yīng)式樣式設(shè)置。
設(shè)置類
設(shè)置樣式
<style lang="scss" scoped> .filmSwiperItem { img { width: 100%; } } </style>
總結(jié)
Vue 漸進(jìn)式JavaScript 框架 基于Vue2的學(xué)習(xí)筆記 - Vue Swiper組件實(shí)現(xiàn)筆記
到此這篇關(guān)于Vue Swiper組件的文章就介紹到這了,更多相關(guān)Vue Swiper組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue從一個(gè)頁面跳轉(zhuǎn)到另一個(gè)頁面并攜帶參數(shù)的解決方法
這篇文章主要介紹了vue從一個(gè)頁面跳轉(zhuǎn)到另一個(gè)頁面并攜帶參數(shù)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08vue3+element-plus?Dialog對話框的使用與setup?寫法的用法
這篇文章主要介紹了vue3+element-plus?Dialog對話框的使用?與?setup?寫法的使用,本文通過兩種方式結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04Vue通過for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例
今天小編就為大家分享一篇Vue通過for循環(huán)隨機(jī)生成不同的顏色或隨機(jī)數(shù)的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11vue style屬性設(shè)置背景圖片的相對路徑無效的解決
這篇文章主要介紹了vue style屬性設(shè)置背景圖片的相對路徑無效的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10一文詳解Vue3中簡單diff算法的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹Vue3中簡單diff算法的實(shí)現(xiàn)與使用,文中的示例代碼講解詳細(xì),具有一定的借鑒價(jià)值,感興趣的可以了解一下2022-09-09vue3(optionApi)使用Element Plus庫沒有效果的解決方式
這篇文章主要介紹了vue3(optionApi)使用Element Plus庫沒有效果的解決方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03vue改變數(shù)據(jù)后數(shù)據(jù)變化頁面不刷新的解決方法
這篇文章主要給大家介紹了關(guān)于vue改變數(shù)據(jù)后數(shù)據(jù)變化頁面不刷新的解決方法,vue比較常見的坑就是數(shù)據(jù)(后臺返回)更新了,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07IE11下處理Promise及Vue的單項(xiàng)數(shù)據(jù)流問題
最近我開發(fā)的公司的競賽網(wǎng)站被發(fā)現(xiàn)在IE11下排行榜無數(shù)據(jù),但是在其他瀏覽器沒問題,我然后打開控制臺一看,發(fā)現(xiàn)有問題,糾結(jié)了半天才搗騰好,下面小編把方案分享處理,供大家選擇2019-07-07