index.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <template>
  2. <div class="block">
  3. <el-form
  4. :model="ruleForm"
  5. :rules="rules"
  6. ref="ruleForm"
  7. label-width="150px"
  8. class="demo-ruleForm"
  9. >
  10. <el-form-item label="藏品名称(套):" prop="name" style="width: 550px">
  11. <el-input
  12. v-model="ruleForm.name"
  13. maxlength="20"
  14. show-word-limit
  15. ></el-input>
  16. </el-form-item>
  17. <el-form-item label="藏品头图 :" prop="avatar">
  18. <el-upload
  19. class="avatar-uploader"
  20. action="https://jsonplaceholder.typicode.com/posts/"
  21. :show-file-list="false"
  22. :on-success="handleAvatarSuccess"
  23. :before-upload="beforeAvatarUpload"
  24. >
  25. <img v-if="imageUrl" :src="imageUrl" class="avatar" />
  26. <i
  27. v-else
  28. class="el-icon-plus avatar-uploader-icon"
  29. ></i> </el-upload></el-form-item
  30. ></el-form>
  31. </div>
  32. </template>
  33. <script>
  34. export default {
  35. data() {
  36. return {
  37. imageUrl: "",
  38. ruleForm: {
  39. name: "",
  40. img: "",
  41. },
  42. rules: {
  43. name: [
  44. { required: true, message: "请输入藏品名称", trigger: "blur" },
  45. {
  46. min: 2,
  47. max: 20,
  48. message: "长度在 2 到 20个字符",
  49. trigger: "blur",
  50. },
  51. ],
  52. avatar: [
  53. {
  54. required: true,
  55. message: "请按照规范上传尺寸为750px*750px的图片",
  56. trigger: "blur",
  57. },
  58. ],
  59. },
  60. };
  61. },
  62. methods: {
  63. handleAvatarSuccess(res, file) {
  64. this.imageUrl = URL.createObjectURL(file.raw);
  65. },
  66. beforeAvatarUpload(file) {
  67. const isJPG = file.type === "image/jpeg";
  68. const isLt2M = file.size / 1024 / 1024 < 2;
  69. if (!isJPG) {
  70. this.$message.error("上传头像图片只能是 JPG 格式!");
  71. }
  72. if (!isLt2M) {
  73. this.$message.error("上传头像图片大小不能超过 2MB!");
  74. }
  75. return isJPG && isLt2M;
  76. },
  77. },
  78. };
  79. </script>
  80. <style lang="scss" scoped>
  81. .block {
  82. margin: 20px 100px;
  83. padding: 60px;
  84. border: 1px solid #ebebeb;
  85. border-radius: 5px;
  86. }
  87. </style>
  88. <style>
  89. .avatar-uploader .el-upload {
  90. border: 1px dashed #d9d9d9;
  91. border-radius: 6px;
  92. cursor: pointer;
  93. position: relative;
  94. overflow: hidden;
  95. }
  96. .avatar-uploader .el-upload:hover {
  97. border-color: #409eff;
  98. }
  99. .avatar-uploader-icon {
  100. font-size: 28px;
  101. color: #8c939d;
  102. width: 300px;
  103. height: 300px;
  104. line-height: 300px;
  105. text-align: center;
  106. }
  107. .avatar {
  108. width: 300px;
  109. height: 300px;
  110. display: block;
  111. }
  112. </style>