data() { return { upload_filelist_{{_object}}: [], } }, methods: { upload_delete(e) { this[`upload_filelist_${e.name}`].splice(e.index, 1) // 编辑模式下如果删除,应该将form中的数据也删除 }, async upload_afterread(e) { let lists = [].concat(e.file) let fileListLen = this[`upload_filelist_${e.name}`].length lists.map((item) => { this[`upload_filelist_${e.name}`].push({ ...item, status: 'uploading', message: '上传中' }) }) for (let i = 0; i < lists.length; i++) { let result = await this.upload_promise(lists[i].url) result = this.__API__+'/storage/' + result let item = this[`upload_filelist_${e.name}`][fileListLen] this[`upload_filelist_${e.name}`].splice(fileListLen, 1, Object.assign(item, { status: 'success', message: '', url: result })) fileListLen++ } }, upload_promise(url) { uni.showLoading({ title: '上传中,请稍后', mask: true }) return new Promise((resolve, reject) => { let a = uni.uploadFile({ url: `${this.__API__}/base/upload.html`, filePath: url, name: 'file', formData: {user: 'test'}, success: (res) => { // setTimeout(() => { // resolve(res.data.data) // }, 1000) // 此处要注意返回值是否是JSON格式 let data = JSON.parse(res.data) setTimeout(() => { uni.hideLoading() resolve(data.data.file) }, 1000) } }); }) }, } ------------------------ 注意事项 ------------------------ // 提交数据时获取组件中上传的图片数据 submit() { this.form.{{_object}} = this.upload_filelist_{{_object}}.length > 0 ? this.upload_filelist_{{_object}}[0].url : '' this.form.{{_object}} = this.form.{{_object}}.replace(this.__API__+'/storage/', '') // 无论添加还是编辑模式,此处都要去除添加的路径 }, // 编辑模式下显示默认值 this.upload_filelist_{{_object}} = [{url:this.__API__+'/storage/'+res.data.data.{{_object}}}]