首页 > 解决方案 > 从 dropzone.js 参数访问数据属性 - vuejs

问题描述

我正在尝试从 dropzoneOption 访问数据参数,如下所示:

data() {
  return {
    e1: 0,
    dropzoneOptions: {
      url: '/api/imageUpload',
      thumbnailWidth: null,
      thumbnailHeight: null,
      acceptedFiles: '.jpg, .jpeg, .png',
      autoProcessQueue: false,
      previewsContainer: '.preview-container',
      init: function() {
        this.on("addedfile", function(file) { 
          //Access to e1 params.
          //This.e1 not working
      });
      },
    }
  }
},

有没有办法从函数内部访问它?

标签: vue.jsparametersdropzone.js

解决方案


你有没有试过这样写:

init: function () {
 const _this = this //assigning this as a variable
  this.on("addedfile", function(file) {
     console.log("Added file ", file); 
     console.log(_this.e1)
  });
}

您还可以挂钩到 dropzone 事件:

    
[...]
fileAdded: {
    sendingEvent (file) {
      // access this.e1 here
    }
  }
<vue-dropzone v-on:vdropzone-file-added="fileAdded"></vue-dropzone>


推荐阅读