【问题标题】:Can't access data() in onUploadProgress event (axios)无法在 onUploadProgress 事件中访问 data() (axios)
【发布时间】:2021-03-05 07:13:00
【问题描述】:

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

<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();
      }
}

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

为什么?

【问题讨论】:

标签: javascript vue.js axios nuxt.js


【解决方案1】:

绑定你的 vue 实例:

    await this.$axios.post(this.uploadUrl, data, {
        onUploadProgress: function (progressEvent) {
            console.log(this.isOnUploading); 
        }.bind(this)
    });

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2018-09-07
    • 2017-04-26
    • 1970-01-01
    • 2020-05-09
    • 2019-12-06
    • 2021-10-23
    • 1970-01-01
    • 2018-08-28
    相关资源
    最近更新 更多