upload.vue 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <template>
  2. <div id="upload">
  3. <el-upload
  4. v-if="url"
  5. ref="upload"
  6. :action="url"
  7. :list-type="listType"
  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,.mp4"
  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. listType: { type: String, default: 'picture-card' },
  34. },
  35. components: {},
  36. data: () => ({
  37. dialogVisible: false,
  38. dialogImageUrl: '',
  39. fileList: [],
  40. }),
  41. created() {
  42. if (this.data) {
  43. this.defalutProcess(this.data);
  44. }
  45. },
  46. watch: {
  47. data: {
  48. handler(val) {
  49. this.defalutProcess(val);
  50. },
  51. },
  52. },
  53. computed: {},
  54. methods: {
  55. handlePictureCardPreview(file) {
  56. this.dialogImageUrl = file.url;
  57. this.dialogVisible = true;
  58. },
  59. handleRemove(file) {
  60. return true;
  61. },
  62. outLimit() {
  63. this.$message.error('只允许上传1张图片');
  64. },
  65. onSuccess(response, file, fileList) {
  66. //将文件整理好传回父组件
  67. this.$emit('upload', { type: this.type, data: response });
  68. },
  69. defalutProcess(val) {
  70. this.$set(this, `fileList`, [{ name: this.type, url: `${this.data}?${new Date().getTime()}` }]);
  71. },
  72. },
  73. };
  74. </script>
  75. <style lang="less" scoped></style>