2

Parent Component contains one Address Component:

// ParentComponent.vue
<Address ref="childAddress"></Address>

Address Component contains many contact components :

// AddressComponent.vue
<template>
  <div>
    <Contact ref="childContact"></Contact>
    <Contact ref="childContact"></Contact>
    <Contact ref="childContact"></Contact>
  </div>
</template>
export default {
  components: { Contact },
  data: () => ({
    address_inputs: [],
  }),
  methods: {
    getAddresses() {
      // RETURN address_inputs
    }
  }
}

Contact Component container:

// ContactComponent.vue
    <template>
      <div>
        contact component
      </div>
    </template>
    export default {
      components: { Contact },
      data: () => ({
        contact_inputs: [],
      }),
      methods: {
        getContacts() {
          // RETURN contact_inputs
        }
      }
    }

What I am trying to do:

I am trying to get address_inputs[] from AddressComponent and contact_inputs[] from ContactComponent in the ParentComponent.

What I have tried:

I have tried to call getAddresses in AddressComponent using: this.$refs.childAddress.getAddresses();

And this works fine but I could not then access the ContactComponent contact_input[] value.

2
  • 1
    i think that Vuex is suitable for this case Commented Oct 22, 2018 at 22:56
  • I am using Vuex but to store my API values and state but in this case I just want object from child component to parent Commented Oct 22, 2018 at 23:34

1 Answer 1

1

$refs attribute is feature that allow you to manipulate DOM for example change div color so you are going wrong way.

If you want pass data from parent to component you should use props. If you want recive data from child to parent you have to use events.

In your example you are trying to pass data from child to parent so in your child component write input change method which emit event for example: this.$emit('testEvent', dataFromInput);

End then in your parent template you can catch it

<Contact ref="childContact" @testEvent="printData"></Contact>

and in parent js part

methods: {
  printData(dataFromInput) {
    console.log(dataFromInput);
  }
}

that's all.

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

2 Comments

I am using the Contact inputs and store them in an object so how I pass this object using events?
You can use watch and on value change trigger event with data. watch: { contact_inputs: function (val) { this.$emit('testEvent', val); },

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.