index.vue 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. >
  18. <div v-if="tip" slot="tip" class="el-upload__tip">{{ tip }}</div>
  19. <i slot="default" class="el-icon-plus" />
  20. </el-upload>
  21. <el-dialog :visible.sync="dialogVisible">
  22. <img width="100%" :src="dialogImageUrl" />
  23. </el-dialog>
  24. </div>
  25. </template>
  26. <script>
  27. // import { mapGetters } from 'vuex'
  28. export default {
  29. name: 'SimpleUploadImage',
  30. props: {
  31. tip: {
  32. type: String,
  33. default: '',
  34. },
  35. multiple: {
  36. type: Boolean,
  37. default: false,
  38. },
  39. limit: {
  40. type: Number,
  41. default: 1,
  42. },
  43. accept: {
  44. type: String,
  45. default: '.jpg,.png,.gif',
  46. },
  47. listType: {
  48. type: String,
  49. default: 'picture-card',
  50. },
  51. imageList: {
  52. type: Array,
  53. default: () => [],
  54. },
  55. uuid: {
  56. type: Number,
  57. default: 0,
  58. },
  59. beforeUpload: {
  60. type: Function,
  61. default: () => true,
  62. },
  63. },
  64. data() {
  65. return {
  66. dialogVisible: false,
  67. dialogImageUrl: '',
  68. }
  69. },
  70. computed: {
  71. // ...mapGetters(['token']),
  72. chooseState() {
  73. return this.imageList.length < this.limit
  74. },
  75. action() {
  76. return process.env.VUE_APP_UPLOAD_API + '/upload/image'
  77. },
  78. headers() {
  79. return {
  80. // 'X-Token': this.token,
  81. }
  82. },
  83. },
  84. methods: {
  85. // 上传成功
  86. uploadImageSuccess(response, file, fileList) {
  87. this.$emit('success', { response, file, fileList })
  88. },
  89. // 删除
  90. handleImageRemove(file, fileList) {
  91. this.$emit('remove', { file, fileList })
  92. },
  93. // 上传失败
  94. uploadError(err, file, fileList) {
  95. this.$emit('error', { err, file, fileList })
  96. },
  97. handlePictureCardPreview(file) {
  98. this.dialogImageUrl = file.url
  99. this.dialogVisible = true
  100. },
  101. },
  102. }
  103. </script>