index.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <template>
  2. <div>
  3. <el-upload
  4. :class="{ 'el-upload-hidden': !chooseState }"
  5. :list-type="listType"
  6. :action="action"
  7. :headers="headers"
  8. :on-success="uploadImageSuccess"
  9. :on-remove="handleImageRemove"
  10. :before-upload="beforeUpload"
  11. :on-error="uploadError"
  12. :on-preview="handlePictureCardPreview"
  13. :limit="limit"
  14. :multiple="multiple"
  15. :accept="accept"
  16. :file-list="imageList"
  17. :disabled="disabled"
  18. >
  19. <div v-if="tip" slot="tip" class="el-upload__tip">{{ tip }}</div>
  20. <i slot="default" class="el-icon-plus" />
  21. </el-upload>
  22. <el-dialog :visible.sync="dialogVisible">
  23. <img width="100%" :src="dialogImageUrl" />
  24. </el-dialog>
  25. </div>
  26. </template>
  27. <script>
  28. // import { mapGetters } from 'vuex'
  29. export default {
  30. name: 'SimpleUploadImage',
  31. props: {
  32. tip: {
  33. type: String,
  34. default: '',
  35. },
  36. multiple: {
  37. type: Boolean,
  38. default: false,
  39. },
  40. limit: {
  41. type: Number,
  42. default: 1,
  43. },
  44. accept: {
  45. type: String,
  46. default: '.jpg,.png,.gif',
  47. },
  48. listType: {
  49. type: String,
  50. default: 'picture-card',
  51. },
  52. imageList: {
  53. type: Array,
  54. default: () => [],
  55. },
  56. uuid: {
  57. type: Number,
  58. default: 0,
  59. },
  60. beforeUpload: {
  61. type: Function,
  62. default: () => true,
  63. },
  64. disabled: {
  65. type: Boolean,
  66. default: false,
  67. },
  68. },
  69. data() {
  70. return {
  71. dialogVisible: false,
  72. dialogImageUrl: '',
  73. }
  74. },
  75. computed: {
  76. // ...mapGetters(['token']),
  77. chooseState() {
  78. return this.imageList.length < this.limit
  79. },
  80. action() {
  81. return process.env.BASE_URL + '/upload/image'
  82. },
  83. headers() {
  84. return {
  85. // 'X-Token': this.token,
  86. }
  87. },
  88. },
  89. methods: {
  90. // 上传成功
  91. uploadImageSuccess(response, file, fileList) {
  92. this.$emit('success', { response, file, fileList })
  93. },
  94. // 删除
  95. handleImageRemove(file, fileList) {
  96. this.$emit('remove', { file, fileList })
  97. },
  98. // 上传失败
  99. uploadError(err, file, fileList) {
  100. this.$emit('error', { err, file, fileList })
  101. },
  102. handlePictureCardPreview(file) {
  103. this.dialogImageUrl = file.url
  104. this.dialogVisible = true
  105. },
  106. },
  107. }
  108. </script>
  109. <style scoped lang="scss">
  110. ::v-deep {
  111. .el-upload-hidden {
  112. .el-upload {
  113. display: none;
  114. }
  115. }
  116. }
  117. </style>