Menu
Pinkesh's Notes
  • Home
  • Crossword
  • Technology
  • Site Map
  • Contact Me
  • About Me
Pinkesh's Notes

VueJS: Pass data from child component to parent component.

Posted on August 15, 2019August 15, 2019 by jainpinkesh
  1. Create prop in child component:
    props: { childDataProp: { type: Array, default () => []}}
  2. Create a data variable:
    data(){ return {childData:[]};}
  3. Populate value for the data variable created above.
    childData = [1,2,3,4,5];
  4. Use $emit to expose an event as the first argument and data as the second argument:
    this.$emit(‘eventName’,childData);
  5. In Parent Component:
    a. In return object create a variable
    data(){
    parentData:[]
    }
    b. set emitted eventName to the child component’s v-on binding directive like: v-on:eventName
    c. assign an event to the binding, set in step b like this: v-on:eventName=”onEvent”
    c. define onEvent as a method in parent component and pass the data array passed in step 4 (this.$emit(‘eventName’,childData);)
    methods:(){
    onEvent(childDatachildData){}
    }
    d. set someData to the data passed into onEvent method
    methods:(){
    onEvent(childData){
    this.parentData = childData;
    }
    }
    And you should be able to use this.parentData in parent component

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recent Posts

  • VueJS: Call child component’s method from a parent
  • VueJS: Unknown custom element
  • VueJS: Pass data from child component to parent component.
  • Economics – 12th HSC
  • Thoughts and It’s effects

Categories

  • Crossword
  • General
  • Technology


©2026 Pinkesh's Notes | WordPress Theme by Superb Themes