How can I update data from parent component when I click child component on the vue component?

霸气de小男生 提交于 2019-12-12 04:50:15

问题


My first component (child component) like this :

<template>
    ...
</template>
<script>
    export default {
        ...
        methods: {
            addPhoto() {
                const data = { id_product: this.idProduct}
                const item = this.idImage
                this.$store.dispatch('addImage', data)
                    .then((response) => {
                         this.$parent.$options.methods.createImage(item, response)
                    });
            },
        } 
    }
</script>

If the method addPhoto called, it will call ajax and then it will get response ajax

I want to send response ajax and another parameter to method createImage. Method createImage is located in parent component (second component)

My second component (parent component) like this :

<template>
    <div>
        <ul class="list-inline list-photo">
            <li v-for="item in items">
                <div v-if="clicked[item]">
                    <img :src="image[item]" alt="">
                    <a href="javascript:;" class="thumb-check"><span class="fa fa-check-circle"></span></a>
                </div>
                <a v-else href="javascript:;" class="thumb thumb-upload"
                   title="Add Photo">
                    <span class="fa fa-plus fa-2x"></span>
                </a>
            </li>
        </ul>
    </div>
</template>
<script>
    export default {
        ...
        data() {
            return {
                items: [1,2,3,4,5],
                clicked: [], // using an array because your items are numeric
                test: null
            }
        },
        methods: {
            createImage(item, response) {
                console.log(item)
                this.$set(this.clicked, item, true)
                this.test = item
            },
        }
    }
</script>

If the code executed, it success call createImage method on parent component. The console log display value of item

But my problem is the data on parent component not success updated

How can I solve this problem?


回答1:


You really should get in the habit of using events instead of directly accessing the parent component from a child.

In your case, it would be simple to emit an event in the then handler of the child's async request:

.then((response) => {
  this.$emit('imageAdded', item);
});

And listen for it in the parent scope:

<child-component @itemAdded="createImage"></child-component>

That way, any component that uses that child component can react to its imageAdded event. Plus, you won't ever need to spend time debugging why the createImage method is firing when it's never being called in the Vue instance.


Your code isn't working because the way you are invoking the createImage method means that the this inside the function will not be referencing the parent component instance when it is called. So setting this.clicked or this.test will not affect the parent instance's data.

To call the parent component's function with the right context, you would need to do this:

this.$parent.createImage(item, response)


来源:https://stackoverflow.com/questions/45108107/how-can-i-update-data-from-parent-component-when-i-click-child-component-on-the

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!