uploadImg.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <template>
  2. <view class="upload">
  3. <view @click="previewImage(index)" v-for="(item,index) in list" :key="index" class="upload-item">
  4. <image style="width: 100%;height: 100%;" :src="item" mode=""></image>
  5. <view @click.stop="deleteImg(item,index)" class="upload-item-icon">
  6. <uni-icons type="closeempty" size="20" color="#fff"></uni-icons>
  7. </view>
  8. </view>
  9. <view v-if="this.list.length < 9" @click="addImg"
  10. style="display: flex;justify-content: center;align-items: center;" class="upload-item">
  11. <uni-icons type="plusempty" size="40" color="#999"></uni-icons>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. props: {
  18. list: {
  19. type: Array,
  20. default: () => []
  21. }
  22. },
  23. watch: {
  24. },
  25. data() {
  26. return {
  27. }
  28. },
  29. methods: {
  30. addImg() {
  31. //_self为在export default外面作用域外定义的全局变量,用来等价代换
  32. //第一步:打开手机相册或者文件管理器选择文件
  33. uni.chooseImage({
  34. count: 9, //允许上传一张图片
  35. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
  36. sourceType: ['album', 'camera'], //从相册选择
  37. success: (res) => {
  38. //获得选择好的文件,就算只有一个文件也是数组
  39. const tempFilePaths = res.tempFilePaths;
  40. this.$emit('change', res.tempFilePaths)
  41. }
  42. })
  43. },
  44. previewImage(index) {
  45. uni.previewImage({
  46. current: index,
  47. urls: this.list
  48. });
  49. },
  50. deleteImg(item, index) {
  51. this.$emit('deleteImg', index)
  52. }
  53. },
  54. }
  55. </script>
  56. <style lang="scss" scoped>
  57. .upload {
  58. width: 100%;
  59. display: flex;
  60. flex-wrap: wrap;
  61. }
  62. .upload-item {
  63. width: 200rpx;
  64. height: 200rpx;
  65. border: 1px solid #ccc;
  66. border-radius: 5px;
  67. margin-right: 4rpx;
  68. margin-bottom: 36rpx;
  69. position: relative;
  70. .upload-item-icon {
  71. position: absolute;
  72. right: -20rpx;
  73. top: -20rpx;
  74. width: 60rpx;
  75. height: 60rpx;
  76. text-align: center;
  77. line-height: 60rpx;
  78. background-color: limegreen;
  79. border-radius: 50%;
  80. z-index: 1;
  81. }
  82. }
  83. .upload-item:nth-child(3n) {
  84. margin-right: 0;
  85. }
  86. </style>