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



第一張圖片是上傳之前,第二張圖片是上傳成功后,第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: "上傳文件大小不能超過(guò) 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)文章
解決electron打包vue-element-admin項(xiàng)目頁(yè)面無(wú)法跳轉(zhuǎn)的問(wèn)題小結(jié)
這篇文章主要介紹了解決electron打包vue-element-admin項(xiàng)目頁(yè)面無(wú)法跳轉(zhuǎn)的問(wèn)題小結(jié),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03
vue組件vue-treeselect箭頭和叉圖標(biāo)變大問(wèn)題及解決
這篇文章主要介紹了vue組件vue-treeselect箭頭和叉圖標(biāo)變大問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-07-07
vue-meta實(shí)現(xiàn)router動(dòng)態(tài)設(shè)置meta標(biāo)簽的方法
這篇文章主要介紹了vue-meta實(shí)現(xiàn)router動(dòng)態(tài)設(shè)置meta標(biāo)簽,實(shí)現(xiàn)思路非常簡(jiǎn)單內(nèi)容包括mata標(biāo)簽的特點(diǎn)和mata標(biāo)簽共有兩個(gè)屬性,分別是http-equiv屬性和name屬性,本文通過(guò)實(shí)例代碼給大家詳細(xì)講解需要的朋友可以參考下2022-11-11
vue等兩個(gè)接口都返回結(jié)果再執(zhí)行下一步的實(shí)例
這篇文章主要介紹了vue等兩個(gè)接口都返回結(jié)果再執(zhí)行下一步的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09

