我在用el-dialog对话框使用表单el-form提交数据,发现在用el-upload上传头像时,头像没有立即显示出来,我明明已经把后台返回的url渲染到了界面上
<el-upload action="" ref="upload" :show-file-list="false" :auto-upload="false" :limit="1" :on-change="handleChange" > <img v-if="userData.picture" :src="userData.picture" class="avatar" /> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload>
我思考了会,猜测是在中,已经弹出显示的对话框不能根据数据刷新节点。
需要加上以下代码
this.$forceUpdate()
全部代码是:
handleChange(file) { let _this = this this.fileTemp = file.raw let fd = new FormData() fd.append('file', this.fileTemp) //传文件 if (this.fileTemp) { upload(fd).then(res => { this.$forceUpdate() this.userData.picture = res.data.data // console.log('1111', this.fileTemp) }) } },
还有就是在上传图片时,刚开始我用的是http:request:
| 覆盖默认的上传行为,可以自定义上传的实现,然后我发现this.$refs.upload.clearFiles()他并不难清空我上传后的文件,于是又使用了on-change事件。 |