Vue.js + Element-ui Upload:如何调用方法 ClearFiles 或 Abort

1 javascript upload vuejs2 element-ui

我正在使用vue.jselement-ui

我想使用该upload-file组件将文件发送到我的服务器。

如果这样的文件已经存在,我想停止将文件加载到服务器。我想使用声称的方法abortor clearFiles,但不能。我的错误是什么?

HTML

<el-upload
     action="",
     :http-request="addAttachment",
     :on-remove="deleteAttachment",
     :before-upload="handleBeforeUpload",
     :file-list="fileList">
</el-upload>
<el-button size="small" type="success" @click="clearFiles">clear</el-button>
Run Code Online (Sandbox Code Playgroud)

爪哇脚本

var vm = new Vue({
    data() {
        return {
            fileList: []
        };
    },
    methods: {
        handleBeforeUpload(file) {
            //if loading file exists in fileList - abort 
            if (findIndexInFileList(file) >= 0) {
                clearFiles(); //error
                this.clearFiles(); //.. and error
                vm.clearFiles(); //.. and also error
            }
        }
    }
}).$mount('#app');

vm.clearFiles(); //error!
Run Code Online (Sandbox Code Playgroud)

小智 7

与裁判!将 ref 属性添加到 el-upload

<el-upload
 ref="upload">
</el-upload>
Run Code Online (Sandbox Code Playgroud)

然后通过 $refs 对象调用

this.$refs.upload.clearFiles()
Run Code Online (Sandbox Code Playgroud)

  • 万一有人像我一样在一年后发现这个:fileList 仅保存默认值而不是您上传的文件。clearFiles() 似乎只是清理 UI (4认同)