- 创建一个UploadFile.vue组件文件 复制下方代码 并修改上传接口api
<template>
<div>
<div class="img-up">
<div
class="img-list"
@mouseenter="imgShow = index"
@mouseleave="imgShow = ''"
v-for="(item, index) in fileList"
:key="index"
>
<img :src="baseUrl + item.url.replace('file', '')" class="avatar" />
<transition name="el-zoom-in-bottom">
<div class="img-tools" v-show="imgShow === index">
<i class="el-icon-view img-view " @click="handlePreview(item)" ></i>
<i class="el-icon-delete img-del " @click="beforeRemove(item)"></i></div
></transition>
</div>
<el-upload
action="#"
ref="upload"
accept="image/*"
:http-request="handleFileUpload"
:on-preview="handlePreview"
:on-remove="handleRemove"
:before-upload="beforeAvatarUpload"
:on-exceed="handleExceed"
:limit="3"
:show-file-list="false"
>
<i v-if="fileList.length < 3" class="el-icon-plus img-btn"></i>
</el-upload>
</div>
<el-dialog
append-to-body
:visible.sync="dialogVisible"
style="z-index: 2005"
>
<img width="100%" :src="dialogImageUrl" alt="" />
</el-dialog>
</div>
</template>
<script>
import { uploadFile } from "@/api/jjjManage/jjjNews.js";
export default {
props: {
value: {
type: String,
},
},
data() {
return {
loading: false,
fileList: [],
baseUrl: APP_PUBLIC_CONFIG.IMG_IRL,
dialogImageUrl: "",
dialogVisible: false,
imgShow: "",
};
},
watch: {
value(v) {
this.fileList = this.getFlieList(v);
},
},
mounted() {},
methods: {
getFlieList(list) {
if (!list) return [];
return list.split(",").map((e) => {
return {
name: e,
url: e,
};
});
},
beforeAvatarUpload(file) {
if (this.fileList > 3) {
this.handleExceed(this.fileList)
return
}
const isLt2M = file.size / 1024 / 1024 < 5;
const isJPG = ["image/jpeg", "image/png"];
if (!isLt2M) {
this.$message.error("上传图片大小不能超过 5MB!");
return false;
}
if (!isJPG.includes(file.type)) {
this.$message.error("上传图片只能是 JPG和PNG 格式!");
return false;
}
},
handleRemove(file, fileList) {
this.$emit("upSuccess", this.fileList.filter((e) => e.name != file.name).map((e) => e.url).toString());
},
handlePreview(file) {
console.log(file)
this.dialogImageUrl = this.baseUrl + file.url.replace('file', '');
this.dialogVisible = true;
},
handleExceed(files ) {
this.$message.warning(
`当前限制选择 3 个文件,本次选择了 ${files.length} 个文件 `
);
},
beforeRemove(file, fileList) {
return this.$confirm(`确定移除 ${file.name}?`).then(_ => {
this.handleRemove(file)
});
},
handleFileUpload(file) {
this.loading = true;
let form = new FormData();
form.append("file", file.file);
uploadFile(form).then((res) => {
let param = res.data.data;
this.fileList.push({ url: param, name: file.file.name });
this.$emit("upSuccess", this.fileList.map((e) => e.url).toString());
this.loading = false;
});
},
},
};
</script>
<style scoped lang="scss">
.img-up {
display: flex;
.avatar,
.img-btn {
width: 100px;
height: 100px;
margin-right: 10px;
}
.img-list {
position: relative;
.img-tools {
width: 100px;
height: 100px;
background: rgba($color: #000000, $alpha: 0.5);
position: absolute;
left: 0;
top: 0;
display: flex;
align-items: center;
justify-content: center;
}
.img-del,.img-view {
color: #fff;
font-size: 20px;
cursor: pointer;
}
.img-view{
margin-right: 15px;
}
}
}
.img-btn {
width: 100px;
height: 100px;
border: 1px solid #eee;
display: flex;
justify-content: center;
align-items: center;
}
.transition-box {
margin-bottom: 10px;
width: 200px;
height: 100px;
border-radius: 4px;
background-color: #409EFF;
text-align: center;
color: #fff;
padding: 40px 20px;
box-sizing: border-box;
margin-right: 20px;
}
</style>
- 组件调用
<template>
<UploadFile :value="imgList" @upSuccess="(e) => {imgList = e;}"/>
<template />
<script>
import UploadFile from "@/components/UploadFile.vue";
export default {
components: { UploadFile },
data() {
return {
imgList:''
}
}
}
<script/>