vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳
1、在確定區(qū)域添加 @paste="_handlePaste"方法;
當(dāng)用戶通過(guò)瀏覽器的用戶界面發(fā)起“粘貼”動(dòng)作時(shí),將觸發(fā) paste 事件。
如果光標(biāo)處于可編輯的上下文中,事件處理器可以通過(guò)調(diào)用事件 clipboardData 屬性的 getData() 方法來(lái)訪問(wèn)剪貼板內(nèi)容。
<el-form :model="dialogForm" label-width="100px" :rules="rules" ref="dialogFormRef" @paste="_handlePaste"> <el-form-item label="上傳圖片" prop="imageList"> <el-upload ref="uploadRef" multiple name="files" v-model:file-list="pictureList" :action="uploadAction" :headers="uploadHeader" list-type="picture-card" :on-preview="handlePictureCardPreview" :on-success="handlePictureSuccess" :on-remove="handleRemove" > <div class="picture-upload"> <el-icon><Plus /></el-icon> <span>上傳圖片</span> </div> </el-upload> <el-dialog v-model="dialogVisible" class="image-preview"> <img w-full :src="dialogImageUrl" alt="Preview Image" /> </el-dialog> </el-form-item> </el-form>
2、ts部分
在頁(yè)面部分 v-model:file-list="pictureList" 使用v-model,刪除和成功上傳的方法可以不使用
import { ref, getCurrentInstance } from 'vue' import type { UploadProps, UploadUserFile, FormRules, FormInstance, ElUpload } from 'element-plus' import { getCookieToken } from '@/utils/util'//獲取token方法 import axios from 'axios' const instance = getCurrentInstance() as any const uploadAction = ref(`接口地址`) const uploadHeader = ref({ token: getCookieToken('token') }) const dialogForm = ref({ imageList: [], // 圖片列表 }) const pictureList = ref<UploadUserFile[]>([])// 圖片列表 const rules = reactive<FormRules>({ imageList: [{ required: true, message: '請(qǐng)上傳', trigger: ['blur', 'change'] }] }) const dialogFormRef = ref<FormInstance>() const uploadRef = ref<InstanceType<typeof ElUpload>>() const dialogImageUrl = ref('') const dialogVisible = ref(false) // 圖片刪除 const handleRemove: UploadProps['onRemove'] = () => { } // 圖片預(yù)覽 const handlePictureCardPreview: UploadProps['onPreview'] = (uploadFile) => { dialogImageUrl.value = uploadFile.url! dialogVisible.value = true } // 上傳成功 const handlePictureSuccess:UploadProps['onSuccess'] = (e, f) => { } // 獲取圖片參數(shù)列表--v-model:file-list="pictureList" ,可獲取想要的圖片參數(shù) const _getPictureList = () => { const tempImgList = ref([]as any) if (pictureList.value.length) { pictureList.value.forEach((element:any) => { if (element.response) { tempImgList.value.push(element?.response?.data[0].url) } else { tempImgList.value.push(element.url) } }) dialogForm.value.imageList = tempImgList.value } } //監(jiān)聽粘貼操作 const _handlePaste = (e:any) => { let clipboardData = e.clipboardData if (!clipboardData) { // chrome clipboardData = e.originalEvent.clipboardData } const items = ref(''as any) items.value = (e.clipboardData || (window as any).clipboardData).items const file = ref(null as any) if (!items.value || items.value.length === 0) { ElMessage.error('當(dāng)前瀏覽器不支持粘貼本地圖片,請(qǐng)打開圖片復(fù)制后再粘貼!') return } for (let i = 0; i < items.value.length; i++) { // 限制上傳文件類型 if (items.value[i].type.indexOf('image') !== -1) { file.value = items.value[i].getAsFile() break } } if (file.value) { const fileData = reactive({ file: ''as any }) fileData.file = file.value _uploadfile(fileData)//調(diào)用上傳接口 } } //調(diào)用上傳接口 const _uploadfile = (fileObj:any) => { const formData = new FormData() formData.set('files', fileObj.file) // 這里使用封裝的上傳文件的接口 axios.post('接口地址', formData, { headers: { token: getCookieToken('token')//獲取請(qǐng)求頭 } }).then(res => { pictureList.value.push(res.data.data[0]) instance.proxy.$forceUpdate()//強(qiáng)制更新 }).catch((e) => { console.log(e, '報(bào)錯(cuò)') }) }
就可以實(shí)現(xiàn)粘貼上傳了
到此這篇關(guān)于vue3中el-uplod結(jié)合ts實(shí)現(xiàn)圖片粘貼上傳的文章就介紹到這了,更多相關(guān)vue3 ts 圖片粘貼上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Vue Echarts渲染數(shù)據(jù)導(dǎo)致殘留臟數(shù)據(jù)的問(wèn)題處理
- vue2.0中自適應(yīng)echarts圖表、全屏插件screenfull的使用
- Vue3+TS項(xiàng)目中eslint、prettier安裝配置詳細(xì)指南
- vue3+ts實(shí)現(xiàn)一個(gè)表單組件的詳細(xì)代碼
- Vue3 Echarts通用的折線圖帶陰影效果實(shí)現(xiàn)
- 在導(dǎo)入.vue文件的時(shí)候,ts報(bào)錯(cuò)提示:找不到模塊“@/Layout/index.vue”或其相應(yīng)的類型聲明問(wèn)題
相關(guān)文章
vue3.x?的shallowReactive?與?shallowRef?使用場(chǎng)景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場(chǎng)景,感興趣的朋友一起看看吧2025-02-02vue實(shí)現(xiàn)的上傳圖片到數(shù)據(jù)庫(kù)并顯示到頁(yè)面功能示例
這篇文章主要介紹了vue實(shí)現(xiàn)的上傳圖片到數(shù)據(jù)庫(kù)并顯示到頁(yè)面功能,結(jié)合實(shí)例形式分析了基于vue.js的數(shù)據(jù)庫(kù)操作及頁(yè)面圖片顯示相關(guān)操作技巧,需要的朋友可以參考下2018-03-03解決新建一個(gè)vue項(xiàng)目過(guò)程中遇到的問(wèn)題
這篇文章主要介紹了解決新建一個(gè)vue項(xiàng)目過(guò)程中遇到的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10Vue3?實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能(刷新保持狀態(tài))
倒計(jì)時(shí)的運(yùn)用場(chǎng)景是需要經(jīng)常用到的,但是根據(jù)業(yè)務(wù)的不同,好比手機(jī)驗(yàn)證碼或者是郵箱驗(yàn)證碼之類的,即使用戶跳轉(zhuǎn)到其它頁(yè)面或者刷新,再次回到登錄也,驗(yàn)證碼的倒計(jì)時(shí)也得保持狀態(tài),下面通過(guò)本文給大家分享Vue3?驗(yàn)證碼倒計(jì)時(shí)功能實(shí)現(xiàn),感興趣的朋友一起看看吧2022-08-08Vue鼠標(biāo)點(diǎn)擊事件和鍵盤事件舉例詳解
在Vue框架中我們經(jīng)常需要綁定各種JS事件,如"點(diǎn)擊事件"、"鼠標(biāo)移動(dòng)事件"、"鍵盤事件"等等,這篇文章主要給大家介紹了關(guān)于Vue鼠標(biāo)點(diǎn)擊事件和鍵盤事件的相關(guān)資料,需要的朋友可以參考下2024-01-01vue高德地圖JS API實(shí)現(xiàn)海量點(diǎn)標(biāo)記示例
本文主要介紹了vue高德地圖JS API實(shí)現(xiàn)海量點(diǎn)標(biāo)記示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-08-08VUE對(duì)Storage的過(guò)期時(shí)間設(shè)置,及增刪改查方式
這篇文章主要介紹了VUE對(duì)Storage的過(guò)期時(shí)間設(shè)置,及增刪改查方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02