how to set state of parent component vue code example

Example 1: vuejs push data update parent

<!--
 From the child component, you want to do an "emit". This tells the
 parent to run a function which is specified in the child component
 reference. Using Pseudo code here:
-->
<parent>
  <child
    :child-data="data"
    @updateParentData="updateData">
  </child>
  <ChildScript>
    methods: {
      updateParentData() {
    	Vue.$emit('updateParentData', childData);
      }
    }
  </ChildScript>
</parent>
<ParentScript>
  methods: {
    updateData(childData) {
      // Do some stuff with child data
    }
  }
</ParentScript>

Example 2: how to access both child event param and parent param in vue

<template>
    <ul>
        <product v-for="product in products"
                 :product="product"
                 @add="addToChart" />
    </ul>
</template>

<script>
const Product = {
    props: ["product"],
    render(h) {
        return h("li", { on: { click: this.click } }, this.product);
    },
    methods: {
        click() {
            this.$emit("add", { product: this.product, quantity: 42 });
        }
    }
};

export default {
    data() {
        return {
            products: ["Foo", "Bar"]
        };
    },
    components: {
        Product
    },
    methods: {
        addToChart({ product, quantity }) {
            console.log(product, quantity);
        }
    }
}
</script>

Tags:

Html Example