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

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

2.與一般上傳組件的區(qū)別
與餓了么上傳組件主要區(qū)別在于只會(huì)在一個(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('上傳頭像圖片大小不能超過(guò) 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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
VUE3項(xiàng)目VITE打包優(yōu)化的實(shí)現(xiàn)
本文主要介紹了VUE3項(xiàng)目VITE打包優(yōu)化的實(shí)現(xiàn),包括使用視圖分析工具、路由懶加載、第三方庫(kù)CDN引入、gzip壓縮、按需引入第三方庫(kù)、TreeShaking、剔除console和debugger、分包策略和圖片壓縮等,具有一定的參考價(jià)值,感興趣的可以了解一下2025-03-03
ElementUI Tree 樹(shù)形控件的使用并給節(jié)點(diǎn)添加圖標(biāo)
這篇文章主要介紹了ElementUI Tree 樹(shù)形控件的使用并給節(jié)點(diǎn)添加圖標(biāo),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02
Vue3請(qǐng)求后端接口實(shí)現(xiàn)方式
本文詳解Vue3中使用Fetch和Axios與后端交互的方法,涵蓋請(qǐng)求發(fā)起、狀態(tài)管理、錯(cuò)誤處理及跨域等解決方案,推薦優(yōu)先使用Axios,其攔截器、自動(dòng)轉(zhuǎn)換等特性提升開(kāi)發(fā)效率,建議封裝實(shí)例與API函數(shù)以統(tǒng)一配置2025-09-09
一次用vue3簡(jiǎn)單封裝table組件的實(shí)戰(zhàn)過(guò)程
之所以封裝全局組件是為了省事,所有的目的,全都是為了偷懶,下面這篇文章主要給大家介紹了關(guān)于用vue3簡(jiǎn)單封裝table組件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2022-12-12
vite項(xiàng)目如何集成eslint和prettier
這篇文章主要介紹了vite項(xiàng)目如何集成eslint和prettier問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-01-01
Element-UI 多個(gè)el-upload組件自定義上傳不用上傳url并且攜帶自定義傳參(文件序號(hào))
有多個(gè)upload組件,每個(gè)都需要單獨(dú)上傳獲取文件(JS File類(lèi)型),不需要action上傳到指定url,自定義上傳動(dòng)作和http操作,下面通過(guò)本文給大家分享Element-UI 多個(gè)el-upload組件自定義上傳不用上傳url并且攜帶自定義傳參(文件序號(hào)),感興趣的朋友一起看看吧2024-06-06
Vue中 axios delete請(qǐng)求參數(shù)操作
這篇文章主要介紹了Vue中 axios delete請(qǐng)求參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所 幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
vue3組件庫(kù)添加腳本的實(shí)現(xiàn)示例
本文主要介紹了vue3組件庫(kù)添加腳本的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-06-06

