2

First off, I should say that I'm fairly new to Vue.js 2, but so far I love it. I'm using the latest version.

I have started to build my first bigger application but got into a situation right away, where I have a main app component and 2 different sibling components inside the app component.

<template>

    <div>
        <comp1></comp1>
        <comp2></comp2>
    </div>

</template>

<script>

    import Comp1 from './Comp1'
    import Comp2 from './Comp2'

    export default
    {
        components:
        {
            Comp1,
            Comp2
        },
        data: function()
        {
            return {
                
            }
        }
    }

</script>

Here is how Comp1 looks like:

<template>
    
    <div>
        <ul>
            <li @click="doClick(data)">Component 1</li>
        </ul>
    </div>

</template>

<script>
    
    export default
    {
        data: function()
        {
            return {
                data: 123
            }
        }
    }

</script>

...and here is Comp 2:

<template>
    
    <div>
        <ul>
            <li>Component 2: { newData }</li>
        </ul>
    </div>

</template>

<script>
    
    export default
    {
        data: function()
        {
            return {
                newData: null
            }
        },
        methods:
        {
            doClick(data)
            {
                this.newData = data;
            }
        }
    }

</script>

What I would like to do is, fire the doClick event on Comp1 and handle it on Comp2.

What's the most effective was and best practice to achieve the desired result? Do I need Vuex or just need to emit events somehow?

starball
  • 20,030
  • 7
  • 43
  • 238
Radical_Activity
  • 2,618
  • 10
  • 38
  • 70
  • 1
    one approach is ``, another approach is comp1 emit click event, then inside comp2, `this.parent.$on()` – Sphinx Jun 21 '18 at 22:13
  • 1
    Possible duplicate of [Communication between sibling components in VueJs 2.0](https://stackoverflow.com/questions/38616167/communication-between-sibling-components-in-vuejs-2-0) – Jaya Jun 21 '18 at 22:20
  • 1
    You have to think if the communication will happen between multiple components, if later on you are most likely to need a `Comp3` which needs to be aware of the events of the other two components, you should use Vuex. If the communication will only happen between those two components, you can go ahead and emit an event on Comp1, listen to that event in the parent component and change the data property which is used as a prop for Comp2. – Ricky Ruiz Jun 22 '18 at 00:01

1 Answers1

0

I think instead of complicating stuff between Comp1 and Comp2 it's better to just maintain the data in the parent component that holds the Comp1 and Comp2 instead. I will call this component ParentComponent.

This way you keep the real truth in one place. Comp1 doesn't have to save the memory instead referenced the data given by ParentComponent through props and Comp2 doesn't have to save the data saved by Comp1 instead uses props too. Although small difference, if you use this consistently it will create a big difference. Now all you need is to use emit to announce that there's some interaction happens in Comp1 or Comp2. This pattern is usually called Smart and Dumb component or some says "Smart and View" component.

Basically the "Smart" one manages all data and interactions while the "View" component just report to the Smart component. Now the benefit of this is that the "View or Dumb" component can be reused.

After all, if you save it in Comp1 you have to save it in ParentComponent too, and then give it to Comp2. This means you have 3 data located instead of the earlier one where you have 1 data in ParentComponent that were being referenced by Comp1 and Comp2.

Now while others mentioned Vuex, I think this is really an overkill thought. I do love the Context feature in React, and been waiting for it in Vue. It's like a smaller Redux or Vuex and turns out, that's all you need.

Now AFAIK Vue 2 doesn't have that. Maybe there's a similar package out there that can help you. But I won't say much about that, just google it. Feel free to share the package you find thought. I've been using React for the last 2 year, so I didn't have my knowledge updated.

Keeping your code simple is the best practice.

Irfandy Jip
  • 1,308
  • 1
  • 18
  • 36