网站首页 > 开源技术 正文
1.封装头像组件
<template>
<el-upload
class="avatar-uploader"
action=""
:show-file-list="false"
:before-upload="beforeAvatarUpload"
>
<!-- (自动上传)action是上传地址 人资项目不需要 人资项目(手动上传) -->
<!-- show-file-list不展示列表 -->
<img v-if="value" :src="value" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon" />
</el-upload>
</template>
<script>
export default {
props: {
value: {
type: String,
default: ''
}
},
methods: {
// 检查函数 判断文件的类型还有大小 return true(继续上传)/false(停止上传)
beforeAvatarUpload(file) {
// jpeg png gif bmp
const isJPG = ['image/jpeg', 'image/png', 'image/gif', 'image/bmp'].includes(file.type)
const isLt2M = file.size / 1024 / 1024 < 5 // 5M
if (!isJPG) {
this.$message.error('上传头像图片只能是 JPG PNG GIF BMP 格式!')
}
if (!isLt2M) {
this.$message.error('上传头像图片大小不能超过 5MB!')
}
return isJPG && 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;
}
</style>
2.引入组件
3.父组件利用v-modal给子组件传值
<!-- 放置上传图片 -->
<image-upload v-model="userInfo.staffPhoto" />
子组件用props接收
props: {
value: {
type: String,
default: ''
}
},
子组件显示props值value
<img v-if="value" :src="value" class="avatar">
猜你喜欢
- 2024-10-02 拉取远程项目(拉取远程仓库代码)
- 2024-10-02 开源软件分享-VUE后台管理模板(vue后端管理系统)
- 2024-10-02 vue-admin-template请求访问springboot后端时携带token
- 2024-06-22 9.7k star 开源免费且开箱即用的中后台管理模版vue-pure-admin
- 2024-06-22 基于 vue3+element-plus 前端后台管理系统SCUI Admin
- 2024-06-22 Layui Admin 后台模板
- 2024-06-22 基于 Bootstrap v3.3.7的后台管理模板 Light Year Admin
- 2024-06-22 清新优雅&高颜值!一个基于Vue3实现的后台管理模板
- 2024-06-22 Spring Boot + Vue + Shiro 实现前后端分离、权限控制
- 2024-06-22 【开源】后端开发也很容易上手的前端框架模板
你 发表评论:
欢迎- 最近发表
- 标签列表
-
- jdk (81)
- putty (66)
- rufus (78)
- 内网穿透 (89)
- okhttp (70)
- powertoys (74)
- windowsterminal (81)
- netcat (65)
- ghostscript (65)
- veracrypt (65)
- asp.netcore (70)
- wrk (67)
- aspose.words (80)
- itk (80)
- ajaxfileupload.js (66)
- sqlhelper (67)
- express.js (67)
- phpmailer (67)
- xjar (70)
- redisclient (78)
- wakeonlan (66)
- tinygo (85)
- startbbs (72)
- webftp (82)
- vsvim (79)
本文暂时没有评论,来添加一个吧(●'◡'●)