<template> <div> <el-upload list-type="picture-card" class="avatar-uploader" :action="action" :auto-upload="true" :data="uploadData" name="file" :show-file-list="true" :on-success="handleSuccess" :before-upload="beforeUpload" :on-exceed="handleExceed" :on-remove="handleRemove" accept="image/png, image/jpeg, image/gif, image/jpg" :limit="1" > <img v-if="imageUrl" :src="imageUrl" class="avatar" /> <i v-else class="el-icon-plus avatar-uploader-icon"></i> <!-- remove圖標沒有起作用 --> <i slot="default" class="el-icon-plus"></i> <div slot="file" slot-scope="{file}"> <!-- <img class="el-upload-list__item-thumbnail" :src="imageUrl" alt /> --> <span class="el-upload-list__item-actions"> <span v-if="!disabled" class="el-upload-list__item-delete"> <i class="el-icon-delete"></i> </span> </span> </div> </el-upload> </div> </template> <script> export default { data() { return { // action: process.env.VUE_APP_IMG_URL + "/image/upload", action: "http://img.tourdev.cn:8001/nv2/image/upload", disabled: false, uploadData: { file: "", project: "JINXINSERVE_WEB" }, imageUrl: "" }; }, props: { coverUrl: { type: String, default: "" } }, created() { this.imageUrl = this.coverUrl; }, methods: { handleRemove(file) { this.$emit("getUrlFn", ""); }, beforeUpload(file) { this.uploadData.file = file; const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error("上傳頭像圖片大小不能超過 2MB!"); return isLt2M; } }, handleSuccess(res, file) { // this.imageUrl = URL.createObjectURL(file.raw); // 獲取后端對上傳圖片存儲位置的路徑, if (res.code === 200) { // console.log("pic", res); this.imageUrl = ""; // this.$emit("getUrlFn", res.url); this.$emit("getUrlFn", res.browser); } else { this.$message.error("圖片上傳失敗"); } }, handleExceed(files, fileList) { this.$message.error("只能上傳一張圖片"); } } }; </script> <style scope> .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: 148px; height: 148px; line-height: 148px; text-align: center; } .avatar { width: 148px; height: 148px; display: block; } </style>
//顯示編輯圖片
<template> <div> <el-upload class="avatar-uploader" :action="action" :auto-upload="true" :data="uploadData" name="file" :show-file-list="false" :on-success="handleSuccess" :before-upload="beforeUpload" accept="image/png, image/jpeg, image/gif, image/jpg" > <!-- remove圖標沒有起作用 --> <div slot="file" slot-scope="{file}"> <span class="el-upload-list__item-actions"> <span v-if="!disabled" class="el-upload-list__item-delete" @click="handleRemove(file)"> <i class="el-icon-delete"></i> </span> </span> </div> <img v-if="imageUrl" :src="imageUrl" class="avatar" /> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </div> </template> <script> export default { data() { return { action: "http://img.tourdev.cn:8001/nv2/image/upload", disabled: false, uploadData: { file: "", project: "JINXINSERVE_WEB" }, imageUrl: "" }; }, props: { coverUrl: { type: String, default: "" } }, created() {}, watch: { coverUrl(val) { this.imageUrl = val; } }, methods: { handleRemove(file) { console.log(file); }, beforeUpload(file) { this.uploadData.file = file; const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error("上傳頭像圖片大小不能超過 2MB!"); return isLt2M; } }, handleSuccess(res, file) { // this.imageUrl = URL.createObjectURL(file.raw); // 獲取后端對上傳圖片存儲位置的路徑, if (res.code === 200) { this.imageUrl = res.url; this.$emit("getUrlFn", this.imageUrl); } else { this.$message.error("圖片上傳失敗"); } } } }; </script> <style scope> .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: 148px; height: 148px; line-height: 148px; text-align: center; } .avatar { width: 148px; height: 148px; display: block; } </style>