upload.vue 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. <template>
  2. <div id="upload">
  3. <el-upload
  4. v-if="url"
  5. ref="upload"
  6. :action="url"
  7. list-type="picture-card"
  8. :file-list="fileList"
  9. :limit="limit"
  10. :on-exceed="outLimit"
  11. :on-preview="handlePictureCardPreview"
  12. :before-remove="handleRemove"
  13. :on-success="onSuccess"
  14. accept=".jpg,.jpeg,.png,.bmp,.gif,.svg"
  15. >
  16. <template>
  17. <i class="el-icon-plus"></i>
  18. </template>
  19. </el-upload>
  20. <el-dialog :visible.sync="dialogVisible">
  21. <img width="100%" :src="dialogImageUrl" alt="" />
  22. </el-dialog>
  23. </div>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'upload',
  28. props: {
  29. url: { type: null },
  30. limit: { type: Number },
  31. data: { type: null },
  32. type: { type: String },
  33. },
  34. components: {},
  35. data: () => ({
  36. dialogVisible: false,
  37. dialogImageUrl: '',
  38. fileList: [],
  39. }),
  40. created() {
  41. if (this.data) {
  42. this.defalutProcess(this.data);
  43. }
  44. },
  45. watch: {
  46. data: {
  47. handler(val) {
  48. this.defalutProcess(val);
  49. },
  50. },
  51. },
  52. computed: {},
  53. methods: {
  54. handlePictureCardPreview(file) {
  55. this.dialogImageUrl = file.url;
  56. this.dialogVisible = true;
  57. },
  58. handleRemove(file) {
  59. return true;
  60. },
  61. outLimit() {
  62. this.$message.error('只允许上传1张图片');
  63. },
  64. onSuccess(response, file, fileList) {
  65. //将文件整理好传回父组件
  66. this.$emit('upload', { type: this.type, data: response });
  67. },
  68. defalutProcess(val) {
  69. this.$set(this, `fileList`, [{ name: this.type, url: `${this.data}?${new Date().getTime()}` }]);
  70. },
  71. },
  72. };
  73. </script>
  74. <style lang="less" scoped></style>