vue3.x使用swiperUI動態(tài)加載圖片失敗的解決方法
本文實例為大家分享了vue3.x使用swiperUI動態(tài)加載圖片失敗的具體解決方法,供大家參考,具體內(nèi)容如下
版本號:
vue/cli:4.5.12
swiper:^6.8.4
問題
1、動態(tài)加載圖片,但是動態(tài)加載圖片為空,需要顯示默認圖片時使用v-if失效
<div class="swiper-container home_swiper"> ?? ?<div class="swiper-wrapper" v-if="aImages.length > 0"> ?? ??? ?<div class="swiper-slide" v-for="(item,index) in aImages" :key="index"> ?? ??? ??? ?<img :src="item.picUrl" alt="" /> ?? ??? ?</div> ?? ?</div> ?? ?<img v-else src="~@/assets/images/img_001.png" alt="" /> </div>
2、動態(tài)加載圖片,圖片存在時,顯示默認圖片使用v-if會造成dom節(jié)點不刷新
<template v-if="aImages.length > 0"> ?? ?<div class="swiper-container home_swiper"> ?? ??? ?<div class="swiper-wrapper"> ?? ??? ??? ?<div class="swiper-slide" v-for="(item,index) in aImages" :key="index"> ?? ??? ??? ??? ?<img src="~@/assets/images/img_001.png" alt="" /> ?? ??? ??? ?</div> ?? ??? ?</div> ?? ?</div> </template> <img v-else src="默認圖片" alt="" />
解決方案
動態(tài)獲取圖片數(shù)據(jù),圖片不存在時,將默認圖片存入即可,不使用v-if進行判斷
// 部分代碼 import { ?? ?ref, ?? ?nextTick } from 'vue'; import { ?? ?apiImgList } from '@/api/home'; // 默認圖片 import defaultBg from "@/assets/images/img_001.png"; export default { ?? ?setup() { ?? ??? ?const aImages = ref([]); ?? ??? ?// 獲取圖片列表 ?? ??? ?const fGetImgList = () => { ?? ??? ??? ?apiImgList().then(res => { ?? ??? ??? ??? ?aImages = res.result && res.result.length ? res.result : [{ ?? ??? ??? ??? ??? ?picUrl: defaultBg? ?? ??? ??? ??? ?}]; ?? ??? ??? ??? ?nextTick(() => { ?? ??? ??? ??? ??? ?fInitSwiper(); ?? ??? ??? ??? ?}); ?? ??? ??? ?}).catch(() => { ?? ??? ??? ??? ?aImages = [{ ?? ??? ??? ??? ??? ?picUrl: defaultBg? ?? ??? ??? ??? ?}]; ?? ??? ??? ??? ?nextTick(() => { ?? ??? ??? ??? ??? ?fInitSwiper(); ?? ??? ??? ??? ?}); ?? ??? ??? ?}) ?? ??? ?}; ?? ??? ?const fInitSwiper = () => { ?? ??? ??? ?new Swiper(".home_swiper", { ?? ??? ??? ??? ?//循環(huán) ?? ??? ??? ??? ?loop: true, ?? ??? ??? ??? ?//每張播放時長3秒,自動播放 ?? ??? ??? ??? ?spaceBetween: 16, ?? ??? ??? ??? ?// 切換效果? ?? ??? ??? ??? ?effect: "coverflow", ?? ??? ??? ??? ?// 該選項給Swiper用戶提供小小的貼心應(yīng)用,設(shè)置為true時,鼠標覆蓋Swiper時指針會變成手掌形狀,拖動時指針會變成抓手形狀。 ?? ??? ??? ??? ?grabCursor: true, ?? ??? ??? ??? ?// 設(shè)定為true時,active slide會居中,而不是默認狀態(tài)下的居左。 ?? ??? ??? ??? ?centeredSlides: true, ?? ??? ??? ??? ?// 設(shè)置slider容器能夠同時顯示的slides數(shù)量(carousel模式)。 ?? ??? ??? ??? ?slidesPerView: 1.32, ?? ??? ??? ??? ?// 啟動動態(tài)檢查器(OB/觀眾/觀看者),當(dāng)改變swiper的樣式(例如隱藏/顯示)或者修改swiper的子元素時,自動初始化swiper。默認false,不開啟,可以使用update()方法更新。 ?? ??? ??? ??? ?observer: true, ?? ??? ??? ??? ?observeParents: true, ?? ??? ??? ??? ?observeSlideChildren: true, ?? ??? ??? ??? ?// 自動切換 ?? ??? ??? ??? ?autoplay: { ?? ??? ??? ??? ??? ?// 自動切換的時間間隔 ?? ??? ??? ??? ??? ?delay: 3000, ?? ??? ??? ??? ??? ?// 如果設(shè)置為true,當(dāng)切換到最后一個slide時停止自動切換 ?? ??? ??? ??? ??? ?stopOnLastSlide: false, ?? ??? ??? ??? ??? ?// 用戶操作swiper之后,是否禁止autoplay。默認為true:停止 ?? ??? ??? ??? ??? ?disableOnInteraction: false, ?? ??? ??? ??? ?}, ?? ??? ??? ??? ?// 類似于蘋果將多首歌曲的封面以3D界面的形式顯示出來的方式 ?? ??? ??? ??? ?coverflowEffect: { ?? ??? ??? ??? ??? ?// slide做3d旋轉(zhuǎn)時Y軸的旋轉(zhuǎn)角度 ?? ??? ??? ??? ??? ?rotate: 0, ?? ??? ??? ??? ??? ?// 每個slide之間的拉伸值,越大slide靠得越緊。5.3.6 后可使用%百分比 ?? ??? ??? ??? ??? ?stretch: -70, ?? ??? ??? ??? ??? ?// slide的位置深度。值越大z軸距離越遠,看起來越小。 ?? ??? ??? ??? ??? ?depth: 500, ?? ??? ??? ??? ??? ?// depth和rotate和stretch的倍率,相當(dāng)于depth*modifier、rotate*modifier、stretch*modifier,值越大這三個參數(shù)的效果越明顯。 ?? ??? ??? ??? ??? ?modifier: 1, ?? ??? ??? ??? ??? ?// 是否開啟slide陰影 ?? ??? ??? ??? ??? ?slideShadows: true, ?? ??? ??? ??? ?} ?? ??? ??? ?}); ?? ??? ?}; ?? ??? ?return { ?? ??? ??? ?aImages ?? ??? ?} ?? ?} ?? ??? ? }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
vuejs2.0子組件改變父組件的數(shù)據(jù)實例
本篇文章主要介紹了vuejs2.0子組件改變父組件的數(shù)據(jù)實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05Vue項目配置、切換主題顏色詳細教程(mixin+scss方式,簡單高效)
這篇文章主要給大家介紹了關(guān)于Vue項目配置、切換主題顏色(mixin+scss方式)的相關(guān)資料,根據(jù)預(yù)設(shè)的配色方案,在前端實現(xiàn)動態(tài)切換系統(tǒng)主題顏色,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-11-11Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例
本篇主要介紹了Vue導(dǎo)出json數(shù)據(jù)到Excel電子表格的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-12-12Vue.js組件props數(shù)據(jù)驗證實現(xiàn)詳解
這篇文章主要為大家詳細介紹了Vue.js組件props數(shù)據(jù)驗證的實現(xiàn)方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-10-10詳解vue2.0的Element UI的表格table列時間戳格式化
本篇文章主要介紹了詳解vue2.0的Element UI的表格table列時間戳格式化,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-06-06