笔者在用上传模块时存在一个问题,每次带的额外参数在第一次时都为空。因为涉及额外参数所以笔者用的是手动上传。
<el-upload
class="upload-demo"
ref="upload"
action="https://jsonplaceholder.typicode.com/posts/"
:on-preview="handlePreview"
:on-remove="handleRemove"
:file-list="fileList"
:auto-upload="false">
<el-button slot="trigger" size="small" type="primary">选取文件</el-button>
<el-button style="margin-left: 10px;" size="small" type="success" @click="submitUpload">上传到服务器</el-button>
<div slot="tip" class="el-upload__tip">只能上传jpg/png文件,且不超过500kb</div>
</el-upload>
<script>
export default {
data() {
return {
fileList: [{name: 'food.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}, {name: 'food2.jpeg', url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'}]
};
},
methods: {
submitUpload() {
this.$refs.upload.submit();
},
handleRemove(file, fileList) {
console.log(file, fileList);
},
handlePreview(file) {
console.log(file);
}
}
}
</script>
其中有一个按钮submitUpload,笔者认为是在没有提交之前
this.$refs.upload.submit();
可以动态的修改参数。但是每次都是第一次上传后台接口报错,经查询后额外参数为初始值。第二次上传时带的是第一次的参数。
故也就是说,提交按钮模块中修改的代码并没有立刻写入。故修改代码,在选择文件时就添加按钮,然后写入额外参数。
<el-button slot="trigger" size="mini" type="primary" @click="chooseFiles">选取文件</el-button>
然后在后面添加函数即可,确认无误。