0

I have a Vue component with some data inside. I also have an element, which is bind to the data inside the script tag of the component. The problem is that, I use another component to push data to this one, so the div should dynamically change when the new data is pushed. However, that does not happen in my case. Although the data (if I console.log it) changes, the div does not dynamically change as well. Here is the code:

 Main component:
<template>
  <div class="small">
    <div v-text="x">{{x}}</div>
  </div>
</template>

<script>



  export default {
    components: {
      ExternalComponent
    },
    data() {
      return {
        x: 0
      }
    },
    async mounted() {

    },
    methods: {
      receiveData(data) {
        this.x = data;
      }

    }
  }
</script>

<style>
</style>

External component: 
 <template>
      <div class="R">
      </div>
    </template>

    <script>


      import MainComponent from './MainComponent'

      export default {
        components: {
          MainComponent
        },
        data() {
          return {

          }
        },
        async mounted() {

        },
        methods: {
             clickListener() {
                $('.R').on('click', function(event) {
                   MainComponent.methods.receiveData(12)
                });
            }

        }
      }
    </script>

    <style> 
    </style>

Basically, I want that to be pushed and read from the div in the MainComponent each time something is click in the External component ( because different type of data will be sent ).

2
  • 1
    Please post a minimal reproducible example for this. Commented Mar 30, 2020 at 14:13
  • Please guys, does someone have idea? Commented Mar 30, 2020 at 14:53

1 Answer 1

1

You can't pass data between components like that.

For parent -> child communication use props: https://v2.vuejs.org/v2/guide/components.html#Passing-Data-to-Child-Components-with-Props

For child -> parent communication use events: https://v2.vuejs.org/v2/guide/components.html#Listening-to-Child-Components-Events

For more complex communication you can use vuex

Another option for direct method call on child component is through refs: How to access to a child method from the parent in vue.js

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

1 Comment

Okay, thank you! I will try to do it and will let you know if it works.

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.