当前位置:   article > 正文

uniapp,vue3上传图片组件封装

uniapp,vue3上传图片组件封装

首先创建一个 components 文件在里面进行组件的创建 

下面是 vip组件的封装 也就是图片上传组件 只是我的命名是随便起的

  1. <template>
  2. <!--图片 -->
  3. <view class="up-page">
  4. <!--图片-->
  5. <view class="show-box" v-for="(item,index) in imageList" :key="index">
  6. <image class="full" :src="item" :data-src="image" @tap="previewImage(item)">
  7. </image>
  8. <view class="delect-icon" @tap="delect(index)">
  9. <image class="full" :src="clearIcon" mode=""></image>
  10. </view>
  11. </view>
  12. <view v-if="VideoOfImagesShow" @tap="chooseVideoImage" class="box-mode">
  13. <image class="full" :src="selectfile" mode=""></image>
  14. </view>
  15. </view>
  16. </template>
  17. <script setup>
  18. import { ref,onUnmounted } from 'vue';
  19. // 定义响应式数据
  20. const clearIcon = ref('../../static/xxx.png');
  21. const selectfile = ref('../../static/jiahao.png');
  22. const VideoOfImagesShow = ref(true);
  23. const imageList = ref([]);
  24. const videoList = ref([]);
  25. const sourceType = ref(['拍摄', '相册', '拍摄或相册']);
  26. const sourceTypeIndex = ref(2);
  27. const cameraList = ref([
  28. { value: 'back', name: '后置摄像头', checked: 'true' },
  29. { value: 'front', name: '前置摄像头' }
  30. ]);
  31. const cameraIndex = ref(0);
  32. const maxCount = ref(9);
  33. // 生命周期钩子(onMounted, onUnmounted等)
  34. onUnmounted(() => {
  35. imageList.value = [];
  36. sourceTypeIndex.value = 2;
  37. sourceType.value = ['拍摄', '相册', '拍摄或相册'];
  38. });
  39. // 方法
  40. function chooseVideoImage() {
  41. uni.showActionSheet({
  42. title: '选择上传类型',
  43. itemList: ['图片'], // 注意:这里只有'图片',如果需要视频应添加'视频'
  44. success: res => {
  45. if (res.tapIndex === 0) {
  46. chooseImages();
  47. }
  48. // 注意:原代码中没有实现chooseVideo,这里未添加
  49. }
  50. });
  51. }
  52. function chooseImages() {
  53. uni.chooseImage({
  54. count: maxCount.value,
  55. sizeType: ['original', 'compressed'],
  56. sourceType: ['album', 'camera'],
  57. success: res => {
  58. imageList.value = [...imageList.value, ...res.tempFilePaths];
  59. if (imageList.value.length + videoList.value.length === maxCount.value) {
  60. VideoOfImagesShow.value = false;
  61. }
  62. }
  63. });
  64. }
  65. function previewImage(e) {
  66. uni.previewImage({
  67. // current: e.currentTarget.dataset.url || e, // 假设你通过某种方式传递了图片的URL
  68. current: e, // 假设你通过某种方式传递了图片的URL
  69. urls: imageList.value
  70. });
  71. }
  72. // 删除图片的函数
  73. function delect(index) {
  74. uni.showModal({
  75. title: '提示',
  76. content: '是否要删除该图片',
  77. success: res => {
  78. if (res.confirm) {
  79. // 使用splice方法删除图片,注意需要访问.value
  80. imageList.value.splice(index, 1);
  81. if (imageList.value.length+videoList.value.length == maxCount.value) {
  82. VideoOfImagesShow.value = false;
  83. } else {
  84. VideoOfImagesShow.value = true;
  85. }
  86. }
  87. }
  88. });
  89. }
  90. </script>
  91. <style lang="scss">
  92. /* 统一上传后显示的盒子宽高比 */
  93. .box-mode {
  94. width: 27vw;
  95. height: 27vw;
  96. border-radius: 8rpx;
  97. overflow: hidden;
  98. }
  99. .full {
  100. width: 100%;
  101. height: 100%;
  102. }
  103. .up-page {
  104. display: flex;
  105. flex-wrap: wrap;
  106. display: flex;
  107. width: 100%;
  108. .show-box:nth-child(3n){
  109. margin-right: 0;
  110. }
  111. .show-box {
  112. position: relative;
  113. margin-bottom:4vw;
  114. margin-right: 4vw;
  115. @extend .box-mode;
  116. .delect-icon {
  117. height: 40rpx;
  118. width: 40rpx;
  119. position: absolute;
  120. right: 0rpx;
  121. top: 0rpx;
  122. z-index: 1000;
  123. }
  124. }
  125. }
  126. </style>

直接在页面引用

  1. <view class="imgbox">
  2. <view class="example-body">
  3. <!-- <uni-file-picker limit="9" title="最多选择9张图片"></uni-file-picker> -->
  4. <div>选择照片-最多只能选择九张</div>
  5. <vip></vip> //上传图片的组件
  6. </view>
  7. </view>

最终样子

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/861918
推荐阅读
相关标签
  

闽ICP备14008679号