Vue?+?Element?自定義上傳封面組件功能
前一段時(shí)間做項(xiàng)目,頻繁使用到上傳圖片組件,而且只上傳一個(gè)封面,于是想著自定義一個(gè)圖片封面上傳組件。先來看一下效果:



第一張圖片是上傳之前,第二張圖片是上傳成功后,第3張圖片是鼠標(biāo)放上去之后的效果!首先整理需求,圖片上傳我們使用照片墻的方式,只能上傳一張圖片,圖片上傳成功后不能繼續(xù)上傳,如果想要更換圖片,則需要將圖片刪除后重新上傳。點(diǎn)擊圖片上面的放大鏡可以查看大圖。需要限制圖片上傳的格式,圖片的大小。組件代碼:
<template>
<div class="upload">
<el-upload
:class="{'hidden':mFileList.length > 0}"
list-type="picture-card"
:on-remove="handleRemove"
:action="action"
:before-upload="beforeUploadHandle"
:on-success="successHandle"
:on-change="changeHandle"
:limit="1"
:accept="accept"
:on-exceed="handleExceed"
:file-list="fileList"
:on-preview="handlePictureCardPreview"
>
<i class="el-icon-plus"></i>
</el-upload>
<el-dialog :visible.sync="dialogVisible">
<img width="100%" :src="dialogImageUrl" alt="" />
</el-dialog>
</div>
</template>
<script>
export default {
props: {
action: {
type: String,
default: "",
},
accept: {
type: String,
default: "",
},
fileList:{
type: Array,
default: () => [],
},
},
watch: {
fileList(newValue, oldValue) {
this.mFileList = newValue
}
},
data() {
return {
dialogVisible: false, //圖片放大
fileImg: "", //上傳圖片
dialogImageUrl: "", //圖片地址
mFileList:this.fileList,
};
},
methods: {
handleRemove(file, fileList) {
this.$emit("upload-remove", file);
},
handlePictureCardPreview(file) {
this.dialogImageUrl = file.url;
this.dialogVisible = true;
},
// 上傳之前
beforeUploadHandle(file) {
if (file.type !== "image/jpeg" && file.type !== "image/png") {
this.$message({
message: "只支持jpg、png格式的圖片!",
type: "warning",
});
return false;
}
const isLt2M = file.size / 1024 / 1024 < 2;
if (!isLt2M) {
this.$message({
message: "上傳文件大小不能超過 2MB!",
type: "warning",
});
return false;
}
},
// 上傳成功
successHandle(response, file, fileList) {
this.mFileList = fileList;
if (response && response.code === 200) {
this.$message.success("圖片上傳成功!");
this.$emit("upload-success", response, file);
} else {
this.$message.error(response.msg);
}
},
changeHandle(file, fileList) {
if(file.response && file.response.code == 500) {
this.$emit("upload-error",file);
}
},
handleExceed(files, fileList) {
this.$message.warning("只能上傳1張圖片!");
},
},
};
</script>
<style lang="scss">
.upload .hidden .el-upload--picture-card {
display: none;
}
</style>調(diào)用組件代碼:
<template>
<div>
<el-form ref="dataForm" label-width="80px">
<el-form-item label="封面" prop="cover" class="is-required">
<upload list-type="picture-card" :action="url" :accept="'.jpg,.png,.JPG,.PNG'" :fileList="fileList"
:limit="1" @upload-success="uploadFile" @upload-remove="removeFile" @upload-error="uploadError">
</upload>
</el-form-item>
</el-form>
</div>
</template>
<script>
import Upload from '../components/cover-upload/index.vue'
export default {
components: {
Upload
},
data() {
return {
url: "",
fileList: [],
}
},
methods: {
uploadUrl() {
this.url = "http://xxx.xxx.xxx.xxx:xxx/yyxt/admin/course/courseInfo/upload?token=075de0303b15a38833a30a7a3b494794"http://上傳圖片的后臺(tái)接口
},
uploadError(file) {
this.fileList = [];
},
uploadFile(response, file) {
this.fileList = [{
url: response.data,
}, ];
},
removeFile(file) {
this.fileList = [];
},
},
mounted() {
this.uploadUrl();
}
}
</script>點(diǎn)擊上傳后的圖片上的放大鏡,顯示圖片大圖

到此這篇關(guān)于Vue + Element 自定義上傳封面組件的文章就介紹到這了,更多相關(guān)Vue + Element 自定義上傳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue+golang實(shí)現(xiàn)上傳微信頭像功能
這篇文章主要介紹了vue+golang實(shí)現(xiàn)上傳微信頭像功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-10-10
vue項(xiàng)目tween方法實(shí)現(xiàn)返回頂部的示例代碼
這篇文章主要介紹了vue項(xiàng)目tween方法實(shí)現(xiàn)返回頂部,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03
vue本地打開build后生成的dist文件夾index.html問題
這篇文章主要介紹了vue本地打開build后生成的dist文件夾index.html問題,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2019-09-09
vue中element 上傳功能的實(shí)現(xiàn)思路
這篇文章主要介紹了vue中element 的上傳功能的實(shí)現(xiàn)思路,本文大概通過兩種實(shí)現(xiàn)思路,具體內(nèi)容詳情大家跟隨腳本之家小編一起看看吧2018-07-07
elementplus?中?DatePicker?日期選擇器樣式修改無效的問題及解決方案
這篇文章主要介紹了elementplus中DatePicker日期選擇器樣式修改無效的問題,DatePicker日期選擇器彈出面板默認(rèn)掛載在body上,所以在組件中添加了?scoped?屬性的?style?標(biāo)簽下是修改不到其樣式的,講解了datepicker的使用方法,及常見的配置項(xiàng)和對(duì)應(yīng)的值,需要的朋友可以參考下2024-01-01
npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated
安裝vue腳手架報(bào)錯(cuò)可能具體原因比較多,可以根據(jù)報(bào)錯(cuò)信息進(jìn)行排查,本文主要介紹了npm安裝vue腳手架報(bào)錯(cuò)警告npm WARN deprecated,感興趣的可以了解一下2023-11-11

