vue自定義上傳頭像組件功能
1.展示效果
1.1 效果圖

以上是沒有上傳的頁面,以下是上傳后鼠標(biāo)放上去的效果
1.2 效果圖

2.與一般上傳組件的區(qū)別
與餓了么上傳組件主要區(qū)別在于只會在一個(gè)圖像占位符上操作
3.上傳圖片組件
以下是上傳圖片組件代碼內(nèi)容
<template>
<div class="account-avatar">
<el-upload class="avatar-uploader" :action="uploadFileUrl" :show-file-list="false" :headers="headers" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
<img v-if="imageUrl" :src="imageUrl" class="avatar" />
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
<i v-if="imageUrl" class="el-icon-plus avatar-uploader-icon iconClass"></i>
</el-upload>
</div>
</template>
<script>
import { getToken } from "@/utils/auth";
export default {
props: {
imgUrl: {
type: String,
default: ''
}
},
data() {
return {
certificateImg: [],
imageUrl: '',
uploadFileUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上傳的圖片服務(wù)器地址
headers: {
Authorization: "Bearer " + getToken()
},
}
},
mounted() {
this.imageUrl = this.imgUrl
},
methods: {
handleAvatarSuccess(res, file) {
const fileName = file.name
this.imageUrl = file.response.url
this.$emit('fileUrl', this.imageUrl)
},
beforeAvatarUpload(file) {
const isJPG = file.type === 'image/jpeg'
const isPNG = file.type === 'image/png'
const isLt2M = file.size / 1024 / 1024 < 2
if (!isJPG && !isPNG) {
this.$message.error('上傳頭像圖片只能是 JPG或PNG 格式!')
}
if (!isLt2M) {
this.$message.error('上傳頭像圖片大小不能超過 2MB!')
}
return isJPG || (isPNG && isLt2M)
}
}
}
</script>
<style >
.avatar-uploader .el-upload {
border: 1px dashed #d9d9d9;
border-radius: 6px;
cursor: pointer;
position: relative;
overflow: hidden;
}
.avatar-uploader .el-upload:hover {
border-color: #409eff;
}
.avatar-uploader-icon {
font-size: 28px;
color: #8c939d;
width: 178px;
height: 178px;
line-height: 178px;
text-align: center;
}
.avatar {
width: 178px;
height: 178px;
display: block;
}
.iconClass{
position: absolute;
top: 0px;
left: 0px;
opacity: 0;
}
.iconClass:hover{
color: #fbfbfb;
position: absolute;
top: 0px;
left: 0px;
opacity: 0.5;
background-color: #7a7979;
}
</style>4.引用組件
引用組件內(nèi)容代碼
//vue中template部分
<el-form-item prop="issueCardPic" label-width="0">
<ImagesUpdate :imgUrl="form.issueCardPic" class="avatar-uploader"
@fileUrl="avatorUPload"></ImagesUpdate>
</el-form-item>
//vue中script部分
import ImagesUpdate from "@/views/components/imagesUpdate"; //附件組件的地址
export default {
data(){
return{
form:{}
...
}
},
components:{
ImagesUpdate
},
methods:{
avatorUPload(){
//上傳后將地址塞入表單字段
this.$set(this.form, 'issueCardPic', val)
},
}
}到此這篇關(guān)于vue自定義上傳頭像組件的文章就介紹到這了,更多相關(guān)vue自定義上傳頭像組件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue實(shí)現(xiàn)pdf文件發(fā)送到郵箱功能的示例代碼
這篇文章主要介紹了vue實(shí)現(xiàn)pdf文件發(fā)送到郵箱功能,實(shí)現(xiàn)代碼包括對郵箱格式內(nèi)容是否為空的驗(yàn)證方法,代碼簡單易懂,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)圖文詳解
本地開發(fā)一般通過執(zhí)行npm run serve命令來啟動項(xiàng)目,那這行命令到底存在什么魔法?下面這篇文章主要給大家介紹了關(guān)于vue執(zhí)行配置選項(xiàng)npm?run?serve的本質(zhì)的相關(guān)資料,需要的朋友可以參考下2023-05-05
Vue實(shí)現(xiàn)搖一搖功能(兼容ios13.3以上)
這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)搖一搖功能,兼容ios13.3以上,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-01-01
vue項(xiàng)目上傳Github預(yù)覽的實(shí)現(xiàn)示例
這篇文章主要介紹了vue項(xiàng)目上傳Github預(yù)覽的實(shí)現(xiàn)示例,在完成Vue項(xiàng)目以后,在上傳到github并實(shí)現(xiàn)預(yù)覽2018-11-11
vue從零實(shí)現(xiàn)一個(gè)消息通知組件的方法詳解
這篇文章主要介紹了vue從零實(shí)現(xiàn)一個(gè)消息通知組件的方法,結(jié)合實(shí)例形式分析了vue實(shí)現(xiàn)消息通知組件的具體原理、實(shí)現(xiàn)步驟、與相關(guān)操作技巧,需要的朋友可以參考下2020-03-03
vue的圖片需要用require的方式進(jìn)行引入問題
這篇文章主要介紹了vue的圖片需要用require的方式進(jìn)行引入問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
詳解無限滾動插件vue-infinite-scroll源碼解析
這篇文章主要介紹了詳解無限滾動插件vue-infinite-scroll源碼解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-05-05
基于Electron+Vite快速構(gòu)建Vue3桌面應(yīng)用
這篇文章主要介紹了如何基于Electron和Vite快速構(gòu)建Vue3桌面應(yīng)用,本文主要技術(shù)棧就是Vue3、vite、Electron,文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-07-07

