简体   繁体   English

如何更新 Dropzone VueJs 组件上的 prop 选项值?

[英]How do I update prop options value on the Dropzone VueJs component?

I want to update the url option for the dropzone component dynamically later.我想稍后动态更新 dropzone 组件的 url 选项。

Following is the Vue component I have:以下是我拥有的 Vue 组件:

<div id="app">
  <vue-dropzone ref="myVueDropzone" id="dropzone" :options="dropzoneOptions"></vue-dropzone>
  <button @click="updateUrl()">
  Update
  </button>
</div>
<script>

new Vue({
  el: "#app",
  data: {
    dropzoneOptions: {
        url : "https://google.com"
    }
  },
  computed:{  
  },
  components: {
    vueDropzone: vue2Dropzone
  },
  methods: {
    updateUrl(){
      console.log("Updating url")
      this.dropzoneOptions.url = "http://example.com"
    }
  }
})
</script>

When I try to update using the button click, I see that the dropzoneOptions.url has been updated.当我尝试使用按钮单击进行更新时,我看到 dropzoneOptions.url 已更新。 But, when I try uploading files, the files are still posted to the old url https://google.com但是,当我尝试上传文件时,这些文件仍会发布到旧网址https://google.com

Link to JSFiddle: https://jsfiddle.net/1t7L6ouc/3/链接到 JSFiddle: https ://jsfiddle.net/1t7L6ouc/3/

There is an options property that lets you define the url .有一个options属性可让您定义url Since you have given it a ref , you should be able to update it like:既然你给了它一个ref ,你应该能够像这样更新它:

this.$refs.myVueDropzone.dropzone.options.url = 'http://example.com';

You need to make your data reactive.您需要使data具有反应性。

https://v2.vuejs.org/v2/guide/reactivity.html https://v2.vuejs.org/v2/guide/reactivity.html

data: function () {
  
    return {
        dropzoneOptions: {
            url : "https://google.com"
        }
    }

}

If the value still does not change, you may also need to use the set method.如果值仍然没有改变,您可能还需要使用 set 方法。

updateUrl(){
    this.$set(this.dropzoneOptions,'url','http://example.com');
}

https://jsfiddle.net/1t7L6ouc/8/ https://jsfiddle.net/1t7L6ouc/8/

I know it's an old thread but I've come across a similar problem recently, and hopefully my solution might help someone.我知道这是一个旧线程,但我最近遇到了类似的问题,希望我的解决方案可以帮助某人。

I had to post an image to an endpoint looking like api/questions/24/image , 24 being the id of the current "question" being edited.我必须将图像发布到看起来像api/questions/24/image的端点,24 是正在编辑的当前“问题”的 id。 This id would be a store property mapped to my computed properties as questionId .此 id 将是映射到我的计算属性的 store 属性作为questionId Problem is in my case, there was no button to hook an event to.问题是在我的情况下,没有按钮可以挂钩事件。 The images would have to be automatically uploaded.图像必须自动上传。 So the URL I needed to use with dropzone was api/questions/${this.questionId}/image .所以我需要与 dropzone 一起使用的 URL 是api/questions/${this.questionId}/image

As I expected and feared, this URL wouldn't resolve correctly with the documented way of putting dropzoneOptions in the Data property, as in the object, this (in this.questionId ) would no longer be referencing the vue instance.正如我所预料和担心的那样,这个 URL 无法通过将 dropzoneOptions 放在 Data 属性中的记录方式正确解析,就像在对象中一样, this (在this.questionId中)将不再引用 vue 实例。 After playing around with dropzone hooks/events/methods, I ended up setting the whole dropzoneOptions object as a computed property like this:在玩弄了 dropzone 钩子/事件/方法之后,我最终将整个 dropzoneOptions 对象设置为一个计算属性,如下所示:

computed: {
    dropzoneOptions() {
        return {
            acceptedFileTypes: "image/*",
            url: `/api/questions/${this.questionId}/image`, 
            headers: {
                "X-CSRF-TOKEN": document.head.querySelector("[name=csrf-token]").content
            }
        }
    },
   [...]
}

This way, if needed, you can map any parameter to another computed property like so:这样,如果需要,您可以将任何参数映射到另一个计算属性,如下所示:

computed: {
    uploadUrl() {
        return `/api/questions/${this.questionId}/image`;
    },
    dropzoneOptions() {
        return {
            acceptedFileTypes: "image/*",
            url: this.uploadUrl, 
            headers: {
                "X-CSRF-TOKEN": document.head.querySelector("[name=csrf-token]").content
            }
        }
    },
   [...]
}

It doesn't feel like an ideal solution, but that's not too awful either.它感觉不是一个理想的解决方案,但这也不是太糟糕。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM