javascript - this. undefined inside dropzone success callback vuejs2
问题描述
I am using Dropzone.js with Vuejs2. The file upload is working fine however, inside the success
callback function i cannot assign the response to an object of the parent.
Here is a sample of my implementation:
<script>
import vueDropzone from "vue2-dropzone";
export default {
components: {
vueDropzone
},
props: {
},
data: () => ({
data: {
name: ''
},
dropOptions: {
url: "https://httpbin.org/post",
maxFileSize: 10,
autoProcessQueue: false,
success: (file, response) => {
if (response.message == "success"){
this.data.name = response.name;
this.add();
}
else{
console.log('Error Uploading file');
}
}
}),
};
</script>
the error is:
Uncaught TypeError: Cannot set property 'name' of undefined
I tried the solutions found Here but its still not working.
解决方案
看文档,你用错了。
dropOptions
应该只包含它的配置,而不是事件。
在您的情况下,您需要使用 vue-2-dropzones eventsvdropzone-success(file, response)
,因此:
<vue-dropzone
ref="myVueDropzone"
id="dropzone"
:options="dropOptions"
v-on:vdropzone-success="someSuccessMethod">
</vue-dropzone>
...
data() { /* ... */ },
methods: {
someSuccessMethod(file, response) {
// your logic
}
}
推荐阅读
- google-colaboratory - SyntaxError:在 MRJob 上解析时出现意外的 EOF
- xml - Odoo qweb 报告模板奇怪的字符外观
- ios - 我们可以使用 Bitbucket webhook 到 Xcode Server 吗?
- json - JSON Schema 中的有效字符串序列
- graphql - 在 Shopify 批量操作中使用变量 graphql 突变
- freepascal - 在 Lazarus 中,如何散焦 TCustomControl 后代(跨平台)?
- mongodb - 如何执行 mongoDB 查找,然后将结果合并到单个数据集中?
- c# - 无法设置 ASP.NET 类型的数据库初始化程序
- c# - 从列表中获取总和为值的元素数组
- react-native - 标题有问题 apollo grapgql