index.vue 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <template>
  2. <div class="component-upload-image">
  3. <el-upload
  4. :action="uploadImgUrl"
  5. list-type="picture-card"
  6. :on-success="handleUploadSuccess"
  7. :before-upload="handleBeforeUpload"
  8. :on-error="handleUploadError"
  9. name="file"
  10. :show-file-list="false"
  11. :headers="headers"
  12. style="display: inline-block; vertical-align: top"
  13. >
  14. <el-image v-if="!value" :src="value">
  15. <div slot="error" class="image-slot">
  16. <i class="el-icon-plus" />
  17. </div>
  18. </el-image>
  19. <div v-else class="image">
  20. <el-image :src="value" />
  21. <div class="mask">
  22. <div class="actions">
  23. <span title="移除" @click.stop="removeImage">
  24. <i class="el-icon-delete" />
  25. </span>
  26. </div>
  27. </div>
  28. </div>
  29. </el-upload>
  30. </div>
  31. </template>
  32. <script>
  33. import { getToken } from "@/utils/auth";
  34. export default {
  35. data() {
  36. return {
  37. uploadImgUrl: process.env.VUE_APP_BASE_API + "/common/upload", // 上传的图片服务器地址
  38. headers: {
  39. Authorization: "Bearer " + getToken(),
  40. },
  41. };
  42. },
  43. props: {
  44. value: {
  45. type: String,
  46. default: "",
  47. },
  48. },
  49. methods: {
  50. removeImage() {
  51. this.$emit("input", "");
  52. },
  53. handleUploadSuccess(res) {
  54. this.$emit("input", res.url);
  55. this.loading.close();
  56. },
  57. handleBeforeUpload() {
  58. this.loading = this.$loading({
  59. lock: true,
  60. text: "上传中",
  61. background: "rgba(0, 0, 0, 0.7)",
  62. });
  63. },
  64. handleUploadError() {
  65. this.$message({
  66. type: "error",
  67. message: "上传失败",
  68. });
  69. this.loading.close();
  70. },
  71. },
  72. watch: {},
  73. };
  74. </script>
  75. <style scoped lang="scss">
  76. .avatar {
  77. width: 100%;
  78. height: 100%;
  79. }
  80. .image {
  81. position: relative;
  82. .mask {
  83. opacity: 0;
  84. position: absolute;
  85. top: 0;
  86. width: 100%;
  87. background-color: rgba(0, 0, 0, 0.5);
  88. transition: all 0.3s;
  89. }
  90. &:hover .mask {
  91. opacity: 1;
  92. }
  93. }
  94. </style>