蚂蚁设计Vue |上载表格|如何设置initialValue

时间:2020-07-08 20:43:03

标签: vuejs2 antd

我在定义Upload组件中的initialValue时遇到问题,我尝试做的另一件事是使用观察程序并更新formValue和更新道具FileList的方法。 ¿有人知道这是如何工作的吗?

Parent.vue

<Child :file="file"/>
...
async loadFile(item) {
  this.loading = true
  const { data } = await axios(..., {
    ...
    responseType: 'blob',
  })
  const file = new File([data], item.name, { type: data.type });

  this.file= {
    Id: item.id,
    Type: item.attributes.type,
    IsPublic: item.attributes.is_public,
    Descr: item.attributes.descr,
    File: [file]
  }
  this.showForm();
  this.loading = false
},

Children.vue

<a-upload
  :accept="formats"
  :before-upload="beforeUploadEvt"
  :disabled="!formats"
  :remove="removeFileEvt"
  v-decorator="[
    'File',
    {
      valuePropName: 'fileList',
      getValueFromEvent: getValueEvt,
      rules: [{ required: true, message: 'Select a file' }]
    },
  ]" >
  <a-button> <a-icon type="upload" /> Select a file</a-button>
</a-upload>

methods: {
  beforeUploadEvt(file) {
    this.form.setFieldsValue({
      File: [file]
    });
    return false;
  },
  removeFileEvt() {
    this.formulario.setFieldsValue({
      Archivo: []
    });
  },
  getValueEvt(e) {
    if (Array.isArray(e)) {
      return e;
    }
    if(e && e.fileList.length > 1) {
      return e && [e.fileList[1]];
    }
    return e && e.fileList;
  },
},
watch: {
  adjunto: {
    immediate: true,
    deep: true,
    handler(obj) {
      if(obj.File) {
        this.getValueEvt(obj.File);
        //  this.formulario.setFieldsValue({
        //    File: obj.File
        //  });
      }
    }
  }
}

尝试使用我认为最基本的示例,使用属性defaultFileList

<a-upload
  :accept="formats"
  :before-upload="beforeUploadEvt"
  :disabled="!format"
  :remove="removeFileEvt"
  :default-file-list="this.file.File">
  <a-button> <a-icon type="upload" /> Select file</a-button>
</a-upload>

然后,这是我收到的控制台警告和错误,因此似乎与类型有关。 enter image description here

1 个答案:

答案 0 :(得分:0)

如果有人还在为此寻求答案。您不需要加载文件,将数据包装在适当的对象中会有所帮助。 As in this example

fileList: [{
  uid: '-1',
  name: 'image.png',
  status: 'done',
  url: 'https://zos.alipayobjects.com/rmsportal/jkjgkEfvpUPVyRjUImniVslZfWPnJuuZ.png',
}]

<a-upload
  ....
  :file-list="fileList"
>