无法在 onUploadProgress 事件(axios)中访问 data()

data()当我处于 时,我无法访问我的任何数据onUploadProgress。每次都会返回“未定义”。


<template>

    <el-upload ref="upload"></el-upload>

    <el-button type="primary" :loading="isOnUploading" @click="submitUpload">{{ $t('add') }}</el-button>

</template>


export default {

    data() {

      return {

        isOnUploading: false,

    }

},


methods: {

   async uploadFile(event) {

        const data = new FormData();

        data.append('file', event.file);


        await  this.$axios.post(this.uploadUrl, data, {

            onUploadProgress: function (progressEvent) {

                console.log(this.isOnUploading); // Return undefined

            }

        });

    }


     submitUpload() {

        this.$refs.upload.submit();

      }

}

当我调用一个方法时,它给了我同样的问题,它告诉我它是未知的。


为什么 ?


缥缈止盈
浏览 182回答 1
1回答

慕桂英546537

绑定你的 vue 实例:&nbsp; &nbsp; await this.$axios.post(this.uploadUrl, data, {&nbsp; &nbsp; &nbsp; &nbsp; onUploadProgress: function (progressEvent) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(this.isOnUploading);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }.bind(this)&nbsp; &nbsp; });
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript