vue使用Element el-upload 組件踩坑記
一、基本使用
最近研究了一下 el-upload組件 踩了一些小坑 寫起來(lái)大家學(xué)習(xí)一下
很經(jīng)常的一件事情 經(jīng)常會(huì)去直接拷貝 element的的組件去使用 但是用到上傳組件時(shí) 就會(huì)遇到坑了
如果你直接去使用upload 你肯定會(huì)遇見(jiàn)這個(gè)錯(cuò)誤

而且 上傳的圖片 可能會(huì)突然消失 這時(shí)候如果你沒(méi)有接口 你是完全不知道為什么移除的 所以 無(wú)接口時(shí) 只能去猜測(cè)是不是因?yàn)榭缬驁?bào)錯(cuò) 導(dǎo)致圖片消失
最終去拿公司的地址調(diào)完接口,答案是對(duì)的 action="https://jsonplaceholder.typicode.com/posts/" 這是element中的action參數(shù) 用html 時(shí) 他會(huì)去調(diào)用ajax 使同源策略不同導(dǎo)致報(bào)錯(cuò)。
一般呢公司都會(huì)提供一個(gè) 轉(zhuǎn)圖片為url格式的地址鏈接 你只需要去調(diào)用它 保存下來(lái)就好了, 但是可能會(huì)遇到需要token 權(quán)限 ,這時(shí)候很少去做的事情來(lái)了,一般沒(méi)有去直接通過(guò)組件帶token,所以要通過(guò) el-upload組件去攜帶token
<el-upload
action="https://xxxx地址"
:headers="importHeaders"
>
</el-upload>
import {getToken} from '@/utils/token'
data() {
return {
importHeaders: {token: getToken()},
};
},
二、圖片數(shù)量控制
<el-upload
action="https://security.brofirst.cn/api/common/upload"
:headers="importHeaders"
:limit="limit"
:on-exceed="masterFileMax"
>
<i class="el-icon-plus"></i>
</el-upload>
// 最多上傳幾張圖片
masterFileMax(files, fileList) {
console.log(files, fileList);
this.$message.warning(`請(qǐng)最多上傳 ${this.limit} 個(gè)文件。`);
},
三、圖片格式限制/可以選中多張圖片
<el-upload
accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg"
multiple
>
<i class="el-icon-plus"></i>
</el-upload>
例子
<el-upload
action="https://xxxx"
:headers="importHeaders"
list-type="picture-card"
:on-preview="handlePictureCardPreview"
:on-remove="handleRemove"
:on-success="handleAvatarSuccess"
:limit="limit"
:on-exceed="masterFileMax"
accept=".JPG, .PNG, .JPEG,.jpg, .png, .jpeg"
multiple
>
<i class="el-icon-plus"></i>
</el-upload>
<script>
import {getToken} from '@/utils/token'
export default {
name:'feedback',
data() {
return {
importHeaders: {token: getToken()},
images:[],
limit:9
};
},
methods: {
//刪除圖片
handleRemove(file, fileList) {
const idx= this.images.findIndex(it=>it===file.response.data.fullurl)
this.images.splice(idx,1)
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
// 上傳成功后的數(shù)據(jù)
handleAvatarSuccess(response, file, fileList){
console.log(response, file, fileList);
if(response.code===1){
this.images.push(response.data.fullurl)
}
},
// 最多上傳幾張圖片
masterFileMax(files, fileList) {
console.log(files, fileList);
this.$message.warning(`請(qǐng)最多上傳 ${this.limit} 個(gè)文件。`);
}
}
};
</script>

補(bǔ)充:在vue項(xiàng)目中使用element-ui的Upload上傳組件
<el-upload
v-else
class='ensure ensureButt'
:action="importFileUrl"
:data="upLoadData"
name="importfile"
:onError="uploadError"
:onSuccess="uploadSuccess"
:beforeUpload="beforeAvatarUpload"
>
<el-button size="small" type="primary">確定</el-button>
其中importFileUrl是后臺(tái)接口,upLoadData是上傳文件時(shí)要上傳的額外參數(shù),uploadError是上傳文件失敗時(shí)的回掉函數(shù),uploadSuccess是文件上傳成功時(shí)的回掉函數(shù),beforeAvatarUpload是在上傳文件之前調(diào)用的函數(shù),我們可以在這里進(jìn)行文件類型的判斷。
data () {
importFileUrl: 'http:dtc.com/cpy/add',
upLoadData: {
cpyId: '123456',
occurTime: '2017-08'
}
},
methods: {
// 上傳成功后的回調(diào)
uploadSuccess (response, file, fileList) {
console.log('上傳文件', response)
},
// 上傳錯(cuò)誤
uploadError (response, file, fileList) {
console.log('上傳失敗,請(qǐng)重試!')
},
// 上傳前對(duì)文件的大小的判斷
beforeAvatarUpload (file) {
const extension = file.name.split('.')[1] === 'xls'
const extension2 = file.name.split('.')[1] === 'xlsx'
const extension3 = file.name.split('.')[1] === 'doc'
const extension4 = file.name.split('.')[1] === 'docx'
const isLt2M = file.size / 1024 / 1024 < 10
if (!extension && !extension2 && !extension3 && !extension4) {
console.log('上傳模板只能是 xls、xlsx、doc、docx 格式!')
}
if (!isLt2M) {
console.log('上傳模板大小不能超過(guò) 10MB!')
}
return extension || extension2 || extension3 || extension4 && isLt2M
}
}
到此這篇關(guān)于vue使用Element el-upload 組件的文章就介紹到這了,更多相關(guān)vue Element el-upload 組件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- vue3使用el-upload上傳文件示例詳解
- Vue導(dǎo)入excel文件的兩種方式(form表單和el-upload)
- vue使用el-upload實(shí)現(xiàn)文件上傳功能
- vue項(xiàng)目ElementUI組件中el-upload組件與圖片裁剪功能組件結(jié)合使用詳解
- vue+el-upload實(shí)現(xiàn)多文件動(dòng)態(tài)上傳
- vue結(jié)合el-upload實(shí)現(xiàn)騰訊云視頻上傳功能
- vue使用el-upload上傳文件及Feign服務(wù)間傳遞文件的方法
- vue?elementui?實(shí)現(xiàn)圖片上傳后拖拽排序功能示例代碼
- Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實(shí)例
- Vue el-upload單圖片上傳功能實(shí)現(xiàn)
相關(guān)文章
解決父組件將子組件作為彈窗調(diào)用只執(zhí)行一次created的問(wèn)題
這篇文章主要介紹了解決父組件將子組件作為彈窗調(diào)用只執(zhí)行一次created的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07
從零開始實(shí)現(xiàn)Vue簡(jiǎn)單的Toast插件
這篇文章主要給大家介紹了如何從零開始實(shí)現(xiàn)Vue簡(jiǎn)單的Toast插件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
vue使用路由的query配置項(xiàng)時(shí)清除地址欄的參數(shù)案例詳解
這篇文章主要介紹了vue使用路由的query配置項(xiàng)時(shí)如何清除地址欄的參數(shù),本文通過(guò)案例給大家分享完美解決方案,需要的朋友可以參考下2023-09-09
Vue3.0監(jiān)聽器watch與watchEffect詳解
在 Vue 3 中,watch 仍然是一種用于監(jiān)聽數(shù)據(jù)變化并執(zhí)行相應(yīng)操作的方式,不過(guò)在組合式 API 中,watch 的使用方式與選項(xiàng)式 API 略有不同,這篇文章主要介紹了Vue3.0監(jiān)聽器watch與watchEffect,需要的朋友可以參考下2023-12-12
vue3實(shí)現(xiàn)頁(yè)面截圖功能示例詳解
在Vue3項(xiàng)目中實(shí)現(xiàn)頁(yè)面截圖的功能,可以通過(guò)使用js-web-screen-shot組件來(lái)實(shí)現(xiàn),本文以toolbox.js作為案例,詳細(xì)介紹了如何在Vue3框架下,利用js-web-screen-shot組件實(shí)現(xiàn)頁(yè)面截圖的具體步驟和方法,這一技術(shù)的應(yīng)用,不僅可以提高用戶體驗(yàn),還能在需要時(shí)方便地獲取頁(yè)面的即時(shí)信息2024-10-10
對(duì)vue事件的延遲執(zhí)行實(shí)例講解
今天小編就為大家分享一篇對(duì)vue事件的延遲執(zhí)行實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

