Zlpt_Portal/src/components/uploadImg.vue

67 lines
1.5 KiB
Vue
Raw Normal View History

2023-12-01 12:21:43 +08:00
<template>
2023-12-01 14:48:58 +08:00
<div class="uploadImg" :style="`width:${width}px;height:${width}px`">
<el-upload :disabled="disable" class="avatar-uploader" action="#" :show-file-list="false"
:on-change="handleAvatarSuccess">
<img v-if="imageUrl" :src="imageUrl" @click="clickImg" class="avatar">
<!-- <el-icon v-else class="avatar-uploader-icon" :style="`line-height: ${width}px`"><Plus /></el-icon>-->
<el-icon v-else class="avatar-uploader-icon">
<Plus />
</el-icon>
</el-upload>
</div>
2023-12-01 12:21:43 +08:00
</template>
<script lang="ts" setup>
2023-12-01 14:48:58 +08:00
import { ref } from "vue";
import { Plus } from '@element-plus/icons-vue'
2023-12-01 12:21:43 +08:00
2023-12-01 14:48:58 +08:00
const props = defineProps(['disable', 'width'])
const imageUrl = ref('')
2023-12-01 12:21:43 +08:00
2023-12-01 14:48:58 +08:00
const handleAvatarSuccess = (res, file) => {
imageUrl.value = URL.createObjectURL(res.raw);
}
2023-12-01 12:21:43 +08:00
2023-12-01 14:48:58 +08:00
const emit = defineEmits(['onClick'])
2023-12-01 12:21:43 +08:00
2023-12-01 14:48:58 +08:00
const clickImg = () => {
if (!props.disable) {
emit('onClick', {
...this.file,
baseUrl: this.imageUrl
})
2023-12-01 12:21:43 +08:00
}
2023-12-01 14:48:58 +08:00
}
2023-12-01 12:21:43 +08:00
</script>
<style lang="scss" scoped>
2023-12-01 14:48:58 +08:00
.uploadImg {
::v-deep .avatar-uploader {
2023-12-01 12:21:43 +08:00
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
border: 1px solid #dfdfdf;
overflow: hidden;
2023-12-01 14:48:58 +08:00
.el-upload--text {
2023-12-01 12:21:43 +08:00
display: flex;
align-items: center;
justify-content: center;
width: 100%;
height: 100%;
2023-12-01 14:48:58 +08:00
.avatar-uploader-icon {
2023-12-01 12:21:43 +08:00
width: 100%;
height: 100%;
}
}
2023-12-01 14:48:58 +08:00
.avatar {
2023-12-01 12:21:43 +08:00
width: 100%;
height: 100%;
object-fit: cover;
}
}
}
</style>