ImgUploader.vue 949 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <template>
  2. <u-upload :action="action" :file-list="fileList" max-count="1" :after-read="afterRead" @delete="deleteImg" preview-size="160" />
  3. </template>
  4. <script>
  5. export default {
  6. props: {
  7. value: {
  8. type: String,
  9. default: ''
  10. }
  11. },
  12. data() {
  13. return {
  14. fileList: [],
  15. action: 'http://192.168.100.17/index.php/index/index/upload'
  16. };
  17. },
  18. created() {
  19. this.setFile(this.value);
  20. },
  21. methods: {
  22. setFile(value) {
  23. if (value) {
  24. this.fileList = [{ url: value }];
  25. } else {
  26. this.fileList = [];
  27. }
  28. },
  29. afterRead(file) {
  30. console.log(file)
  31. this.$http.uploadFile(file.path).then(res => {
  32. let fileList = [];
  33. fileList.push({ ...file, url: res });
  34. this.fileList = fileList;
  35. this.$emit('input', res);
  36. });
  37. },
  38. deleteImg() {
  39. this.fileList = [];
  40. }
  41. }
  42. };
  43. </script>
  44. <style lang="scss" scoped>
  45. /deep/ .u-list-item {
  46. margin-left: 15px;
  47. width: 160px !important;
  48. height: 160px !important;
  49. }
  50. </style>