<script setup> import VueUploadComponent from "vue-upload-component"; import { upload } from "@/api/conf"; const isUploading = ref(false); const allowedExtensions = ref(["jpg", "jpeg", "png", "gif"]); const maxFileSize = ref(10 * 1024 * 1024); const uploadedFiles = ref([]); function handleInputFilter(uploadData) { // 文件过滤器 if (uploadData.file.type.indexOf("image/") !== 0) { return Promise.reject("只能上传图片文件"); } return Promise.resolve(uploadData); } function handleBeforeUpload(uploadData) { // 开始上传前的回调 this.isUploading = true; return uploadData; } function handleUploading(uploadData) { console.log("uploading", uploadData.progress); } function handleUploadSuccess(uploadData) { console.log("success", uploadData.url); this.uploadedFiles.push({ id: uploadData.response.id, name: uploadData.file.name, size: uploadData.file.size, url: uploadData.response.url, }); this.isUploading = false; } function handleUploadError(uploadData) { console.error("error", uploadData.error); this.isUploading = false; } function fileSize(size) { if (size < 1024) { return size + "B"; } else if (size < 1024 * 1024) { return (size / 1024).toFixed(2) + "KB"; } else { return (size / 1024 / 1024).toFixed(2) + "MB"; } } function deleteFile(index) { this.uploadedFiles.splice(index, 1); } </script> <template> <div> <vue-upload-component :http-client="upload" :multiple="true" :accept="'image/*'" :extensions="allowedExtensions" :maxSize="maxFileSize" @input-filter="handleInputFilter" @before-upload="handleBeforeUpload" @uploading="handleUploading" @upload-success="handleUploadSuccess" @upload-error="handleUploadError" > <template v-slot:default> <div class="upload-area"> <div v-if="!isUploading"> <i class="iconfont icon-add"></i> <span class="text">点击上传</span> </div> <div v-else> <i class="iconfont icon-loading"></i> <span class="text">上传中...</span> </div> </div> </template> </vue-upload-component> <div class="preview-area"> <div v-for="(file, index) in uploadedFiles" :key="file.id"> <img :src="file.url" class="preview-image" /> <div class="preview-name">{{ file.name }}</div> <div class="preview-size">{{ fileSize(file.size) }}</div> <div class="preview-delete" @click="deleteFile(index)">删除</div> </div> </div> </div> </template> 优化一下
时间: 2023-07-19 07:42:27 浏览: 171
vue-router 按需加载 component: () = import() 报错的解决
首先,可以将一些处理函数和变量移出 `setup`,放到 `data()` 或 `methods` 中,以便更好地组织代码。同时,可以使用 `computed` 来计算文件大小,使得代码更加简洁。另外,可以将上传成功后的文件信息作为一个对象,而不是多个独立的属性,这样可以更好地管理文件信息。
修改后的代码如下:
```
<template>
<div>
<vue-upload-component
:http-client="upload"
:multiple="true"
:accept="'image/*'"
:extensions="allowedExtensions"
:maxSize="maxFileSize"
@input-filter="handleInputFilter"
@before-upload="handleBeforeUpload"
@uploading="handleUploading"
@upload-success="handleUploadSuccess"
@upload-error="handleUploadError"
>
<template v-slot:default>
<div class="upload-area">
<div v-if="!isUploading">
<i class="iconfont icon-add"></i>
<span class="text">点击上传</span>
</div>
<div v-else>
<i class="iconfont icon-loading"></i>
<span class="text">上传中...</span>
</div>
</div>
</template>
</vue-upload-component>
<div class="preview-area">
<div v-for="(file, index) in uploadedFiles" :key="file.id">
<img :src="file.url" class="preview-image" />
<div class="preview-info">
<div class="preview-name">{{ file.name }}</div>
<div class="preview-size">{{ fileSize(file.size) }}</div>
<div class="preview-delete" @click="deleteFile(index)">删除</div>
</div>
</div>
</div>
</div>
</template>
<script>
import VueUploadComponent from "vue-upload-component";
import { upload } from "@/api/conf";
export default {
components: {
VueUploadComponent,
},
data() {
return {
isUploading: false,
allowedExtensions: ["jpg", "jpeg", "png", "gif"],
maxFileSize: 10 * 1024 * 1024,
uploadedFiles: [],
};
},
computed: {
fileSize() {
return function(size) {
if (size < 1024) {
return size + "B";
} else if (size < 1024 * 1024) {
return (size / 1024).toFixed(2) + "KB";
} else {
return (size / 1024 / 1024).toFixed(2) + "MB";
}
};
},
},
methods: {
handleInputFilter(uploadData) {
// 文件过滤器
if (uploadData.file.type.indexOf("image/") !== 0) {
return Promise.reject("只能上传图片文件");
}
return Promise.resolve(uploadData);
},
handleBeforeUpload(uploadData) {
// 开始上传前的回调
this.isUploading = true;
return uploadData;
},
handleUploading(uploadData) {
console.log("uploading", uploadData.progress);
},
handleUploadSuccess(uploadData) {
console.log("success", uploadData.url);
const file = {
id: uploadData.response.id,
name: uploadData.file.name,
size: uploadData.file.size,
url: uploadData.response.url,
};
this.uploadedFiles.push(file);
this.isUploading = false;
},
handleUploadError(uploadData) {
console.error("error", uploadData.error);
this.isUploading = false;
},
deleteFile(index) {
this.uploadedFiles.splice(index, 1);
},
},
};
</script>
```
阅读全文