Home  >  Q&A  >  body text

How to pass input data of child component to parent component and store into array in Vue.js?

** For example, here when I click the button I will have one more component which means it will have new data so I want to collect all the information into one when the "Save Data" button is pressed Arrays, I hope, are easy to understand

<Child v-for="count in btnNumber" :key="count" @showData="getElements" />

<v-btn
  color="primary"
  elevation="10"
  class="space"
  large
  @click="duplicateEl"
  >Add Categ & Key</v-btn
>
v-btn
      color="secondary"
      elevation="13"
      class="btnEl"
      dark
      large
      @click="getResult"
      >Save Data</v-btn

** It uses Emit to get the data from my child component

methods:{
               getElements(emitPayload) {
              this.selectedChildCategory = emitPayload.selectedCateg;
              this.selectedChildKey = emitPayload.selectedKey;
              this.selectedChildLanguage = emitPayload.selectedLang;
              this.selectedChildContent = emitPayload.selectedCon;
        }
    }
 duplicateEl() {
  this.btnNumber++;
}
P粉832212776P粉832212776205 days ago421

reply all(2)I'll reply

  • P粉092778585

    P粉0927785852024-03-28 10:14:38

    Try to save the data from which the event was emitted (from getting the element) to a new data variable array and use that array

    
        sssccc

    reply
    0
  • P粉418214279

    P粉4182142792024-03-28 09:10:08

    You can save data on parent component, please see the following code snippet:

    Vue.component('Child', {
      template: `
      
        
          
            
              
              
              
              
              
              
              
              
              
              
            
          
        
      
      `,
      props: ['conte'],
      data() {
        return {
          content: this.conte,
          categories: ['first', 'second', 'third'],
          keys: [1,2,3],
          langs: ['g', 'h', 'j'],
          contents: ['aaa', 'bbb', 'ccc']
        }
      },
      methods: {
       setD() {
          this.$emit('show', this.content);
        },
      },
    })
    
    new Vue({
      vuetify: new Vuetify(),
      el: "#app",
      data() {
        return {
          contentFields: [{id: 0, cat: '', key: '', lang: '', cont: ''}],
          showData: false
        }
      },
      methods: {
        addInput() {
          let newI = this.contentFields.length 
          this.contentFields.push({id: newI, cat: '', key: '', lang: '', cont: ''})
        },
        getElements(e){
          const newData = this.contentFields.map(obj => {
            if(obj.id === e.id) 
               return { ...obj }
            return obj
          });
        },
        getResult() {
          this.showData = !this.showData
        }
      }
    })
    
      
      [email protected]/css/materialdesignicons.min.css" rel="stylesheet">
      [email protected]/dist/vuetify.min.css" rel="stylesheet">
      
    
    
      
    Add Categ & Key Save Data
    {{ contentFields }}
    sssccc sssccc

    reply
    0
  • Cancelreply