uniapp vue3.0+TS 上传单张或多张图片,并且能删除和预览。

一、uniapp vue3.0+TS 上传单张或多张图片,并且能删除和预览。

效果:人菜话不多 先上效果:
在这里插入图片描述

二、代码

1.HTML 上传图片相关代码

代码如下:

<template>
  <view class="images_box">
       <view class="imgBox" v-for="(item, index) in fileList" :key="index">
         <img class="image" :src="item.url" alt="" @click="previewImg(index)" />
         <img @click.stop="delImges(item, index)" class="close" src="@/static/image/base/close.png" alt="删除" />
       </view>
       <view class="iconBox" @click="upload">
         <u-icon name="plus" size="28" color="#438BEF"></u-icon>
       </view>
     </view>
</template>

2.TS上传图片相关代码

代码如下:

<script setup lang="ts">
import { ref, reactive } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { examine } from "@/api";

const fileList = ref<any>([]);
const imgList = ref<any>([]);
const formState = reactive<any>({
  conclusion: "", 
  suggestion: "", 
  task_id: null
});

const upload = () => {
  uni.chooseImage({
    count: 9, // 默认9
    mediaType: ["image"],
    sizeType: ["original", "compressed"], // 可以指定是原图还是压缩图,默认二者都有
    sourceType: ["album", "camera"], // 可以指定来源是相册还是相机,默认二者都有
    success: function (res: any) {
      fileList.value = fileList.value.concat(res.tempFiles); //concat追加到数组
      for (let file in fileList.value) {
        up_img(fileList.value[file]);
      }
    },
  });
};
const up_img = (file: any) => {
  uni.uploadFile({
    url: 'https://prod.XXX.com/XXXXX/XXX/XXXX/',
    filePath: file.path,
    name: 'files',
    header: {
      "Content-Type": "multipart/form-data"
    },
    success: (res: any) => {
      let obj = JSON.parse(res.data);
      imgList.value = [
        ...imgList.value,
        {
          type: obj.files[0].type,
          size: obj.files[0].size,
          name: obj.files[0].name,
          url: 'https://prod.XXXXXX/XXXX/XXXX/XXXXX/' + obj.files[0].name,
        }
      ];
      fileList.value = imgList.value
      uni.showToast({
        title: '上传成功',
        icon: "success",
      });
    },
    fail: (res: any) => {
      uni.showToast({
        title: '上传失败',
        icon: "error",
      });
    }
  })
};
//删除图片
const delImges = async (rowItem: any, index: number) => {
  fileList.value.forEach((item: any) => {
    if (item.name == rowItem.name) {
      fileList.value.splice(index, 1)
    }
  })
};
// 点击图片预览
const previewImg = (index: number) => {
  let imgsArray = fileList.value.map((item: any) => {
    return item.url
  })
  uni.previewImage({
    urls: imgsArray,
    current: index,
    indicator: 'number',
    loop: true
  })
}
onLoad((options: any) => {
  formState.task_id = Number(options.taskId)
});
</script>

3.CSS 上传图片相关代码

代码如下:

 .images_box {
   display: flex;
    justify-items: flex-start;
    flex-wrap: wrap;

   .imgBox {
     width: 120rpx;
     height: 120rpx;
     position: relative;
     margin: 20rpx 20rpx 0 0;

     .image {
       width: 100%;
       height: 100%;
        border-radius: 10rpx;
      }

      .close {
        width: 30rpx;
        height: 30rpx;
        position: absolute;
        top: -15rpx;
        right: -15rpx;
      }
    }

    .iconBox {
      width: 120rpx;
      height: 120rpx;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 10rpx;
      border: 1px dashed gray;
      box-sizing: border-box;
      margin-top: 20rpx;
    }
  }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
uniapp是一个基于Vue.js的跨平台开发框架,而Vue3是Vue.js的最新版本。它们的结合可以带来更好的开发体验和性能优化。uniapp提供了导航栏双页切换的功能,而Vue3的Pinia框架则提供了参数输出和方法调用的功能,还支持模块化和持久化存储。如果您有关于uniappVue3的使用问题,您可以查看uniapp官网获取更详细的信息和示例代码。uniapp在早期版本中开始支持Vue3的使用,并且从HBuilderX 3.2版本开始支持创建基于Vue3的uniapp项目。使用uniappVue3开发项目的流程与常规的Vue3开发类似。您可以按照官方提供的升级指南和使用文档来进行开发。希望以上信息对您有所帮助!<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [uniapp+vue3+pinia框架(模块化+持久化存储)](https://download.csdn.net/download/qq_35079107/87910679)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *2* [uniapp + vue3微信小程序开发(1)框架搭建](https://blog.csdn.net/qq_39404437/article/details/124345386)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] - *3* [uniapp开发:uniapp之切换vue3,一直使用一直爽](https://blog.csdn.net/qq_42961150/article/details/121375073)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 33.333333333333336%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值