upload.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <template>
  2. <view>
  3. <view class="upload" @click="handlerUpload" v-if="imageUrl==null || imageUrl == ''">
  4. <text class="iconfont icon-shangchuantupian"></text>
  5. <text>选择图片</text>
  6. </view>
  7. <view class="upload_image" v-else>
  8. <image :src="imageUrl" style="width: 100%;height: 100%;" mode="aspectFill"></image>
  9. <view class="upload-del" @click="handleDelete">
  10. <text class="iconfont icon-shanchu"></text>
  11. </view>
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. import { uploadFileImage } from '@/services/public.js'
  17. export default {
  18. props: {
  19. imageData: {
  20. type: String,
  21. default: () => ''
  22. }
  23. },
  24. data() {
  25. return {
  26. imageUrl: '',
  27. }
  28. },
  29. watch: {
  30. imageData(val) {
  31. console.log(val)
  32. if (val) {
  33. console.log(val)
  34. this.imageUrl = val
  35. }
  36. }
  37. },
  38. methods: {
  39. async handlerUpload() {
  40. try{
  41. const {data} = await uploadFileImage()
  42. this.imageUrl = JSON.parse(data).data
  43. this.$emit('uploadImg', JSON.parse(data).data)
  44. console.log(this.imageUrl)
  45. }catch(error){
  46. console.log(error)
  47. }
  48. },
  49. handleDelete() {
  50. this.imageUrl = ''
  51. this.$emit('uploadDel', '')
  52. },
  53. }
  54. }
  55. </script>
  56. <style scoped lang="scss">
  57. .upload {
  58. width: 210rpx;
  59. height: 210rpx;
  60. border-radius: 6rpx 6rpx 6rpx 6rpx;
  61. border: 1rpx dotted #B2B2B2;
  62. display: flex;
  63. align-items: center;
  64. flex-direction: column;
  65. font-size: 24rpx;
  66. color: #B2B2B2;
  67. justify-content: space-around;
  68. padding: 40rpx 56rpx;
  69. box-sizing: border-box;
  70. white-space: nowrap;
  71. }
  72. .upload_image {
  73. width: 210rpx;
  74. height: 210rpx;
  75. border: 1rpx dotted #B2B2B2;
  76. position: relative;
  77. .upload-del {
  78. width: 40rpx;
  79. height: 40rpx;
  80. position: absolute;
  81. top: -20rpx;
  82. right: -20rpx;
  83. line-height: 40rpx;
  84. text-align: center;
  85. .iconfont {
  86. font-size: $font-size-40;
  87. color: #F85050;
  88. }
  89. }
  90. }
  91. </style>