1

I want to Change the prop value in a vue component from my vue instance.

So that I can use the title value inside my component.

The component:

<template>
    <vue-dropzone ref="uploadDropzone" id="dropzone" :options="dropzoneOptions" :title="title"></vue-dropzone>
</template>

<script>
import vue2Dropzone from 'vue2-dropzone'
import 'vue2-dropzone/dist/vue2Dropzone.css'

export default {
    name: 'app',

    components: {
        vueDropzone: vue2Dropzone
    },

    props: ['title'], // title shall update so that I can use the value as a param below

    data () {
        return {
            dropzoneOptions: {
                method: 'post',
                title: this.title
            }
        }
    },
}
</script>

The instance in which I Change the title value:

Vue.component('vue-dropzone', require('./components/ImageDropzoneComponent.vue'));

const app = new Vue({
    el: '#app',

    data: {
        title: '',
    },

    methods: {
        foo (e) {
            console.log(this.title); // has correct value but how to pass to vue component (prop)
        },
    }
});


<vue-dropzone ref="uploadDropzone" :title="title"></vue-dropzone>
4
  • Show the template that the "instance in which I Change the title value" uses. Commented Mar 10, 2018 at 16:52
  • @acdcjunior added the tag, I am not sure what you mean 100% Commented Mar 10, 2018 at 18:27
  • I see. Your code seems fine. What did you expect that is not happening? Commented Mar 10, 2018 at 19:27
  • @acdcjunior I want to pass the title from the instance to the component Commented Mar 10, 2018 at 19:52

1 Answer 1

1

Consider changing the dropzoneOptions to a computed property, like so.

import vue2Dropzone from 'vue2-dropzone'
import 'vue2-dropzone/dist/vue2Dropzone.css'

export default {
    name: 'app',

    components: {
        vueDropzone: vue2Dropzone
    },

    props: ['title'], // title shall update so that I can use the value as a param below

    computed:
        dropZoneOptions() { // dropZoneOptions recomputes when props.title changes
            return {
                method: 'post',
                title: this.title
            }
        }
    },
}

Alternatively, you may consider a watcher and modify the data on a change, but with the original code posted, I believe this may be the best route.

Check out https://v2.vuejs.org/v2/guide/computed.html for info on both computed properties and watchers.

Sign up to request clarification or add additional context in comments.

6 Comments

Does it not work for you? I do not have access to your project. There are dependencies in the project that I haven't seen.
Are you willing to have a look at the source Code it is not much, just the component and the axios method
Honestly, what you are proposing here makes no difference from what he has in the question. You just declared an unused const title = this.title variable and didn't use it. this.title was already present, so it would aready trigger the computation.
@acdcjunior I Keep getting null in my title I dont get it
@acdcjunior, good point. Not sure what I was doing with the variable, but the point of switching it to a computed property was to be reactive to changes.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.