uniapp+vue3實現(xiàn)上傳圖片組件封裝功能
首先創(chuàng)建一個 components 文件在里面進行組件的創(chuàng)建

下面是 vip組件的封裝 也就是圖片上傳組件 只是我的命名是隨便起的
<template>
<!--圖片 -->
<view class="up-page">
<!--圖片-->
<view class="show-box" v-for="(item,index) in imageList" :key="index">
<image class="full" :src="item" :data-src="image" @tap="previewImage(item)">
</image>
<view class="delect-icon" @tap="delect(index)">
<image class="full" :src="clearIcon" mode=""></image>
</view>
</view>
<view v-if="VideoOfImagesShow" @tap="chooseVideoImage" class="box-mode">
<image class="full" :src="selectfile" mode=""></image>
</view>
</view>
</template>
<script setup>
import { ref,onUnmounted } from 'vue';
// 定義響應(yīng)式數(shù)據(jù)
const clearIcon = ref('../../static/xxx.png');
const selectfile = ref('../../static/jiahao.png');
const VideoOfImagesShow = ref(true);
const imageList = ref([]);
const videoList = ref([]);
const sourceType = ref(['拍攝', '相冊', '拍攝或相冊']);
const sourceTypeIndex = ref(2);
const cameraList = ref([
{ value: 'back', name: '后置攝像頭', checked: 'true' },
{ value: 'front', name: '前置攝像頭' }
]);
const cameraIndex = ref(0);
const maxCount = ref(9);
// 生命周期鉤子(onMounted, onUnmounted等)
onUnmounted(() => {
imageList.value = [];
sourceTypeIndex.value = 2;
sourceType.value = ['拍攝', '相冊', '拍攝或相冊'];
});
// 方法
function chooseVideoImage() {
uni.showActionSheet({
title: '選擇上傳類型',
itemList: ['圖片'], // 注意:這里只有'圖片',如果需要視頻應(yīng)添加'視頻'
success: res => {
if (res.tapIndex === 0) {
chooseImages();
}
// 注意:原代碼中沒有實現(xiàn)chooseVideo,這里未添加
}
});
}
function chooseImages() {
uni.chooseImage({
count: maxCount.value,
sizeType: ['original', 'compressed'],
sourceType: ['album', 'camera'],
success: res => {
imageList.value = [...imageList.value, ...res.tempFilePaths];
if (imageList.value.length + videoList.value.length === maxCount.value) {
VideoOfImagesShow.value = false;
}
}
});
}
function previewImage(e) {
uni.previewImage({
// current: e.currentTarget.dataset.url || e, // 假設(shè)你通過某種方式傳遞了圖片的URL
current: e, // 假設(shè)你通過某種方式傳遞了圖片的URL
urls: imageList.value
});
}
// 刪除圖片的函數(shù)
function delect(index) {
uni.showModal({
title: '提示',
content: '是否要刪除該圖片',
success: res => {
if (res.confirm) {
// 使用splice方法刪除圖片,注意需要訪問.value
imageList.value.splice(index, 1);
if (imageList.value.length+videoList.value.length == maxCount.value) {
VideoOfImagesShow.value = false;
} else {
VideoOfImagesShow.value = true;
}
}
}
});
}
</script>
<style lang="scss">
/* 統(tǒng)一上傳后顯示的盒子寬高比 */
.box-mode {
width: 27vw;
height: 27vw;
border-radius: 8rpx;
overflow: hidden;
}
.full {
width: 100%;
height: 100%;
}
.up-page {
display: flex;
flex-wrap: wrap;
display: flex;
width: 100%;
.show-box:nth-child(3n){
margin-right: 0;
}
.show-box {
position: relative;
margin-bottom:4vw;
margin-right: 4vw;
@extend .box-mode;
.delect-icon {
height: 40rpx;
width: 40rpx;
position: absolute;
right: 0rpx;
top: 0rpx;
z-index: 1000;
}
}
}
</style>直接在頁面引用
<view class="imgbox"> <view class="example-body"> <!-- <uni-file-picker limit="9" title="最多選擇9張圖片"></uni-file-picker> --> <div>選擇照片-最多只能選擇九張</div> <vip></vip> //上傳圖片的組件 </view> </view>
最終樣子


到此這篇關(guān)于uniapp,vue3上傳圖片組件封裝的文章就介紹到這了,更多相關(guān)vue3上傳圖片組件封裝內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Vue2(三)實現(xiàn)子菜單展開收縮,帶動畫效果實現(xiàn)方法
這篇文章主要介紹了vue實現(xiàn)收縮展開效果的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-04-04
使用Vue.js報錯:ReferenceError: “Vue is not d
在前端開發(fā)中,ReferenceError: "Vue is not defined" 是一個常見的錯誤,該錯誤通常發(fā)生在項目中未正確引入 Vue.js 框架或代碼配置存在問題時,本篇文章將詳細分析該錯誤的成因,并提供多種解決方案,幫助開發(fā)者快速排查問題,需要的朋友可以參考下2024-12-12
VUE中如何優(yōu)雅實現(xiàn)爺孫組件的數(shù)據(jù)通信
所謂祖孫組件,也就是3層嵌套的組件,下面這篇文章主要給大家介紹了關(guān)于VUE中如何優(yōu)雅實現(xiàn)爺孫組件的數(shù)據(jù)通信的相關(guān)資料,需要的朋友可以參考下2022-04-04
安裝@vue/cli報錯npmERR gyp ERR問題及解決
這篇文章主要介紹了安裝@vue/cli報錯npmERR gyp ERR問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-08-08
解決Vue項目Network:?unavailable的問題
項目只能通過Local訪問而不能通過Network訪問,本文主要介紹了解決Vue項目Network:?unavailable的問題,具有一定的參考價值,感興趣的可以了解一下2024-06-06

