uni有一個自帶的previewImage方法,是預覽圖片的效果,但current屬性在h5下傳入Number不兼容,所以使用數組的方式傳入。
把預覽圖片封裝成一個方法:
1 // 圖片預覽 2 const imgPreview = (list, idx) => { 3 // list:圖片 url 數組 4 if(list && list.length > 0){ 5 uni.previewImage({ 6 current:list[idx], // 傳 Number H5端出現不兼容 7 urls: list 8 }); 9 } 10 }
在頁面用使用這個方法:
photos是接口返回的對象數組,image代表圖片的url地址
1 <view :data-index="index" @click="getPhotoClickIdx" class="" 2 v-for="(item, index) in photos" :key="index"> 3 <img :src="'https:' + item.image" /> 4 <p> 我是圖片 </p> 5 </view> 6 7 <script> 8 export default { 9 data() { 10 return { 11 photos: [], 12 } 13 }, 14 methods: { 15 // 獲得相冊 idx 16 getPhotoClickIdx(e) { 17 let _this = this; 18 let idx = e.currentTarget.dataset.index; 19 let imgs = _this.photos.map((item, index) => { 20 if (item.image.startsWith('http:') || item.image.startsWith('https:')) { 21 return item.image; 22 } 23 return 'https:' + item.image; 24 }); 25 _this.imgPreview(imgs, idx); 26 }, 27 } 28 } 29 </script>