Vue3中使用Ant Design Vue上传多个图片

模板代码:

在这里插入图片描述

定义变量:

在这里插入图片描述

文件限制的函数:

![![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/e0c18855525e450ab73b10e98fbd3103.pn](https://img-blog.csdnimg.cn/direct/80d98d0659f0415eb0b6720f9634460c.png

上传的函数:

在这里插入图片描述

样式函数:

在这里插入图片描述

完整代码:

<template>
  <div class="dialog-upload" v-if="showUploadDialog">
    <div class="dialog-upload__head">
      <div class="title">上传图片</div>
      <div class="close" @click="closeDialog"></div>
    </div>
    <div class="dialog-upload__body">
        <div class="upload-box">
          <span class="text">tab:</span>
          <div class="pic-box">
            <div v-for="(item,index) in imgUrl1" :key="index" class="pic-box__single">
              <a-image class="pic" :src="item" />
              <div @click="deleteImg('tab',item)" class="pic-delete"></div>
            </div>
            <a-upload
              action="#"
              :multiple="true"
              list-type="picture"
              :before-upload="beforeUpload"
              :customRequest="handleChange"
              :show-upload-list="false"
            >
              <div v-if="!imgUrl1.length || imgUrl1.length <5" class="img" @click="uploadImg('tab')"></div>
            </a-upload>
          </div>
        </div>
        <div class="upload-box">
          <span class="text">tab1:</span>
          <div class="pic-box">
            <div v-for="(item,index) in imgUrl2" :key="index" class="pic-box__single">
              <a-image class="pic" :src="item" />
              <div @click="deleteImg('tab1',item)" class="pic-delete"></div>
            </div>
            <a-upload
              action="#"
              :multiple="true"
              list-type="picture"
              :before-upload="beforeUpload"
              :customRequest="handleChange"
              :show-upload-list="false"
            >
              <div v-if="!imgUrl2.length || imgUrl2.length <5" class="img" @click="uploadImg('tab1')"></div>
            </a-upload>
          </div>
        </div>
    </div>
    <div class="dialog-upload__foot">
        <span class="sure" @click="sure">确定</span>
        <span class="cancle" @click="closeDialog">取消</span>
    </div>
  </div>
</template>

<script lang="ts">
import {
   
    defineComponent, reactive, toRefs, watch, nextTick } from 'vue'
import {
   
    message } from 'ant-design-vue'
import request from '@/request/request'
import axios from 'axios'

export default defineComponent
### 使用 Ant Design Vue 实现图片上传 为了在 Vue 项目中利用 Ant Design Vue 组件库实现图片上传功能,需遵循特定配置流程并编写相应代码。 #### 安装依赖包 首先,在项目根目录执行命令安装 `ant-design-vue` 及其相关依赖项。这一步骤确保开发环境具备所需资源来加载和渲染 Ant Design 的组件[^2]。 ```bash npm install ant-design-vue --save ``` #### 配置全局引入 接着,在项目的入口文件 `main.js` 中按如下方式依次导入整个组件库及其默认样式: ```javascript import { createApp } from &#39;vue&#39;; import App from &#39;./App.vue&#39;; // 引入组件库 import antd from &#39;ant-design-vue&#39;; // 引入样式表 import &#39;ant-design-vue/dist/antd.css&#39;; const app = createApp(App); app.use(antd); // 注册所有组件 app.mount(&#39;#app&#39;); ``` #### 创建上传组件实例 定义一个新的单文件组件用于处理具体的上传逻辑。此部分展示了如何设置 `<a-upload>` 标签属性以及关联事件处理器函数,从而构建一个简易却实用的图像上传界面[^5]。 ```html <template> <div id="uploader"> <!-- 图片卡片形式展示 --> <a-upload name="avatar" listType="picture-card" class="img-upload" :multiple="false" :beforeUpload="handleBeforeUpload" :customRequest="handleCustomRequest" :fileList="imageFiles" @preview="handlePreview" @change="handleChange" @remove="handleRemove" > <div v-if="imageFiles.length < 1"> <a-icon type="plus"/> <div>点击上传</div> </div> </a-upload> <!-- 查看大图模态框 --> <a-modal :visible="modalVisible" footer="" @cancel="closeModal"> <img alt="预览" style="width: 100%" :src="currentImageUrl"/> </a-modal> </div> </template> <script lang="ts"> export default { data() { return { imageFiles: [], // 存储已选中的文件列表 modalVisible: false, // 控制查看器显示状态 currentImageUrl: &#39;&#39;, // 当前正在查看的大图链接 }; }, methods: { handleBeforeUpload(file) { const isValidFileType = file.type.startsWith(&#39;image/&#39;); if (!isValidFileType) { this.$message.error(`${file.name} 文件类型不合法`); return false; } const isLt2M = file.size / 1024 / 1024 < 2; if (!isLt2M) { this.$message.error(`图片 ${file.name} 太大 (超过2MB)!`); return false; } return true; }, async handleCustomRequest(option) { try { let formData = new FormData(); formData.append(&#39;file&#39;, option.file); await fetch(&#39;/api/upload&#39;, { method: &#39;POST&#39;, body: formData, }); option.onSuccess?.(); // 成功回调 } catch(error){ console.log(error); option.onError?.(); // 错误回调 } }, handleChange(info) { switch (info.file.status) { case &#39;uploading&#39;: break; case &#39;done&#39;: this.imageFiles.push({...info.file}); break; case &#39;error&#39;: this.$message.error(`${info.file.name} 上传失败.`); break; } }, handlePreview(file) { this.currentImageUrl = file.url || file.thumbUrl; this.modalVisible = true; }, closeModel() { this.modalVisible = false; }, handleRemove(file) { this.imageFiles = this.imageFiles.filter(item => item.uid !== file.uid); } } }; </script> <style scoped> /* 自定义样式 */ .img-upload .ant-upload-select-picture-card i { font-size: 32px; color: #999; } .img-upload .ant-upload-select-picture-card div { margin-top: 8px; color: #666; } </style> ``` 上述代码片段实现了基本的图片上传功能,并提供了简单的前端验证机制以保证用户体验流畅性。同时,通过自定义请求方法可以灵活对接不同的后端接口完成实际的数据传输操作[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ᥬ 小月亮

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值