Communication between sibling components in Vue.js 2.0
Asked Answered
H

7

171

Overview

In Vue.js 2.x, model.sync will be deprecated.

So, what is a proper way to communicate between sibling components in Vue.js 2.x?


Background

As I understand Vue.js 2.x, the preferred method for sibling communication is to use a store or an event bus.

According to Evan (creator of Vue.js):

It's also worth mentioning "passing data between components" is generally a bad idea, because in the end the data flow becomes untrackable and very hard to debug.

If a piece of data needs to be shared by multiple components, prefer global stores or Vuex.

[Link to discussion]

And:

.once and .sync are deprecated. Props are now always one-way down. To produce side effects in the parent scope, a component needs to explicitly emit an event instead of relying on implicit binding.

So, Evan suggests using $emit() and $on().


Concerns

What worries me is:

  • Each store and event has a global visibility (correct me if I'm wrong);
  • It's too wasteful to create a new store for each minor communication;

What I want is to some scope events or stores visibility for siblings components. (Or perhaps I didn't understand the above idea.)


Question

So, what is the correct way to communicate between sibling components?

Hodgkinson answered 27/7, 2016 at 14:40 Comment(1)
$emit combined with v-model to emulate .sync. i think you should go the Vuex wayNightshade
B
103

With Vue.js 2.0, I'm using the eventHub mechanism as demonstrated in the documentation.

  1. Define centralized event hub.

     const eventHub = new Vue() // Single event hub
    
     // Distribute to components using global mixin
     Vue.mixin({
         data: function () {
             return {
                 eventHub: eventHub
             }
         }
     })
    
  2. Now in your component you can emit events with

     this.eventHub.$emit('update', data)
    
  3. And to listen you do

     this.eventHub.$on('update', data => {
     // do your thing
     })
    

Update

Please see the answer by alex, which describes a simpler solution.

Burrows answered 5/11, 2016 at 20:41 Comment(5)
Just a heads up: keep an eye on Global Mixins, and try to avoid them whenever possible, as according to this link vuejs.org/v2/guide/mixins.html#Global-Mixin they may affect even third party componentes.Vanscoy
A much simpler solution is to use what @Timofei described - this.$root.$emit() and this.$root.$on()Allegory
For future reference, please don't update your answer with someone else's answer (even if you think it's better and you reference it). Link to the alternate answer, or even ask the OP to accept the other one if you think they should - but copying their answer into your own is bad form and discourages users from giving credit where it is due, as they may simply upvote only your answer only. Encourage them to navigate to (and thus upvote) the answer you are referencing by not including that answer in your own.Proclamation
Thank you for the valuable feedback @GrayedFox, updated my answer accordingly.Burrows
Please note that this solution will no longer be supported in Vue 3. See https://mcmap.net/q/142984/-communication-between-sibling-components-in-vue-js-2-0Pedropedrotti
T
229

You can even make it shorter and use the root Vue instance as the global Event Hub:

Component 1:

this.$root.$emit('eventing', data);

Component 2:

mounted() {
    this.$root.$on('eventing', data => {
        console.log(data);
    });
}
Timofei answered 29/10, 2017 at 19:14 Comment(15)
This works better than defining an addition event hub and attaching it to any event-consumer.Deafmute
I'm a big fan of this solution as I really don't like events having scope. However, I don't with VueJS every day so I'm curious if there's anyone out there who sees issues with this approach.Allegory
Simplest solution of all the answersEsta
nice, short and easy to implement, easy to understand as wellHyperborean
Abosulutely genieus of Alex !Selfsacrifice
If you only want exclusively direct siblings comunication, use $parent instead of $rootSlipper
But it fires lots of times instead of one time. if i click on times than it fire event more than one time.Visser
Please note that this solution will no longer be supported in Vue 3. See https://mcmap.net/q/142984/-communication-between-sibling-components-in-vue-js-2-0Pedropedrotti
I have a question to stackoverflow team: Why such an answer, that had been voted 130 up to time I am writing the comment, not ranked first or even second [even the owner of the question sees another answer is better]. I see that this mostly accepted answer is preceded by answers with votes 82, 44 respectively. Thanks in advanceEscadrille
I think $root.$emit won't work if multiple components emitting on the same page using $root. what do you say?Medievalist
@Slipper in this comment thread is giving the correct answer for this specific questionFortepiano
How it could adapted for Vue 3 ?Gyve
How's this genius? correct me if I'm wrong but it seems to have most of the downsides of relying upon a global store (barring hard-coding a variable name), and none of the upsides of keeping these events contained to the relevant components. The idea of using an explicit event-store is you get to choose which component is in charge of encapsulating the communications. e.g. if you had a complex material design select list, that needed some internal sizing information shared.Balderas
This is not working for Vue 3 since $on is removed: v3.vuejs.org/guide/migration/events-api.html#_3-x-updateObellia
This is the best way to communicate with a component that it's not a parent or grandparent. And even then, you should use this to communicate with the grandparent directly. At least, when using vuejs 2.Reeding
B
103

With Vue.js 2.0, I'm using the eventHub mechanism as demonstrated in the documentation.

  1. Define centralized event hub.

     const eventHub = new Vue() // Single event hub
    
     // Distribute to components using global mixin
     Vue.mixin({
         data: function () {
             return {
                 eventHub: eventHub
             }
         }
     })
    
  2. Now in your component you can emit events with

     this.eventHub.$emit('update', data)
    
  3. And to listen you do

     this.eventHub.$on('update', data => {
     // do your thing
     })
    

Update

Please see the answer by alex, which describes a simpler solution.

Burrows answered 5/11, 2016 at 20:41 Comment(5)
Just a heads up: keep an eye on Global Mixins, and try to avoid them whenever possible, as according to this link vuejs.org/v2/guide/mixins.html#Global-Mixin they may affect even third party componentes.Vanscoy
A much simpler solution is to use what @Timofei described - this.$root.$emit() and this.$root.$on()Allegory
For future reference, please don't update your answer with someone else's answer (even if you think it's better and you reference it). Link to the alternate answer, or even ask the OP to accept the other one if you think they should - but copying their answer into your own is bad form and discourages users from giving credit where it is due, as they may simply upvote only your answer only. Encourage them to navigate to (and thus upvote) the answer you are referencing by not including that answer in your own.Proclamation
Thank you for the valuable feedback @GrayedFox, updated my answer accordingly.Burrows
Please note that this solution will no longer be supported in Vue 3. See https://mcmap.net/q/142984/-communication-between-sibling-components-in-vue-js-2-0Pedropedrotti
M
64

Disclaimer: this answer was written a long time ago and it may not reflect latest Vue development or trends. Take everything in this answer with a grain of salt and please comment if you find anything that's outdated, no longer valid, or unhelpful.


State scopes

When designing a Vue application (or in fact, any component based application), there are different types of data that depend on which concerns we're dealing with and each has its own preferred communication channels.

  • Global state: may include the logged in user, the current theme, etc.

  • Local state: form attributes, disabled button state, etc.

Note that part of the global state might end up in the local state at some point, and it could be passed down to child components as any other local state would, either in full or diluted to match the use-case.


Communication channels

A channel is a loose term I'll be using to refer to concrete implementations to exchange data around a Vue app.

Each implementation addresses a specific communication channel, which includes:

  • Global state
  • Parent-child
  • Child-parent
  • Siblings

Different concerns relate to different communication channels.

Props: Direct Parent-Child

The simplest communication channel in Vue for one-way data binding.

Events: Direct Child-Parent

Important notice: $on and $once were removed in Vue version 3.

$emit and v-on event listeners. The simplest communication channel for direct Child-Parent communication. Events enable 2-way data binding.

Provide/Inject: Global or distant local state

Added in Vue 2.2+, and really similar to React's context API, this could be used as a viable replacement to an event bus.

At any point within the components tree could a component provide some data, which any child down the line could access through the inject component's property.

app.component('todo-list', {
  // ...
  provide() {
    return {
      todoLength: Vue.computed(() => this.todos.length)
    }
  }
})

app.component('todo-list-statistics', {
  inject: ['todoLength'],
  created() {
    console.log(`Injected property: ${this.todoLength.value}`) // > Injected property: 5
  }
})

This could be used to provide global state at the root of the app, or localized state within a subset of the tree.

Centralized store (Global state)

Note: Vuex 5 is going to be Pinia apparently. Stay tuned. (Tweet)

Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

And now you ask:

[S]hould I create vuex store for each minor communication?

It really shines when dealing with global state, which includes but is not limited to:

  • data received from a backend,
  • global UI state like a theme,
  • any data persistence layer, e.g. saving to a backend or interfacing with local storage,
  • toast messages or notifications,
  • etc.

So your components can really focus on the things they're meant to be, managing user interfaces, while the global store can manage/use general business logic and offer a clear API through getters and actions.

It doesn't mean that you can't use it for component logic, but I would personally scope that logic to a namespaced Vuex module with only the necessary global UI state.

To avoid dealing with a big mess of everything in a global state, see the Application structure recommandations.

Refs and methods: Edge cases

Despite the existence of props and events, sometimes you might still need to directly access a child component in JavaScript.

It is only meant as an escape hatch for direct child manipulation - you should avoid accessing $refs from within templates or computed properties.

If you find yourself using refs and child methods quite often, it's probably time to lift the state up or consider the other ways described here or in the other answers.

$parent: Edge cases

Similar to $root, the $parent property can be used to access the parent instance from a child. This can be tempting to reach for as a lazy alternative to passing data with a prop.

In most cases, reaching into the parent makes your application more difficult to debug and understand, especially if you mutate data in the parent. When looking at that component later, it will be very difficult to figure out where that mutation came from.

You could in fact navigate the whole tree structure using $parent, $ref or $root, but it would be akin to having everything global and likely become unmaintainable spaghetti.

Event bus: Global/distant local state

See @AlexMA's answer for up-to-date information about the event bus pattern.

This was the pattern in the past to pass props all over the place from far up down to deeply nested children components, with almost no other components needing these in between. Use sparingly for carefully selected data.

Be careful: Subsequent creation of components that are binding themselves to the event bus will be bound more than once--leading to multiple handlers triggered and leaks. I personally never felt the need for an event bus in all the single page apps I've designed in the past.

The following demonstrates how a simple mistake leads to a leak where the Item component still triggers even if removed from the DOM.

// A component that binds to a custom 'update' event.
var Item = {
  template: `<li>{{text}}</li>`,
  props: {
    text: Number
  },
  mounted() {
    this.$root.$on('update', () => {
      console.log(this.text, 'is still alive');
    });
  },
};

// Component that emits events
var List = new Vue({
  el: '#app',
  components: {
    Item
  },
  data: {
    items: [1, 2, 3, 4]
  },
  updated() {
    this.$root.$emit('update');
  },
  methods: {
    onRemove() {
      console.log('slice');
      this.items = this.items.slice(0, -1);
    }
  }
});
<script src="https://unpkg.com/[email protected]/dist/vue.min.js"></script>

<div id="app">
  <button type="button" @click="onRemove">Remove</button>
  <ul>
    <item v-for="item in items" :key="item" :text="item"></item>
  </ul>
</div>

Remember to remove listeners in the destroyed lifecycle hook.


Component types

Disclaimer: the following "containers" versus "presentational" components is just one way to structure a project and there are now multiple alternatives, like the new Composition API that could effectively replace the "app specific containers" I'm describing below.

To orchestrates all these communications, to ease re-usability and testing, we could think of components as two different types.

  • App specific containers
  • Generic/presentational components

Again, it doesn't mean that a generic component should be reused or that an app specific container can't be reused, but they have different responsibilities.

App specific containers

Note: see the new Composition API as an alternative to these containers.

These are just simple Vue component that wraps other Vue components (generic or other app specific containers). This is where the Vuex store communication should happen and this container should communicate through other simpler means like props and event listeners.

These containers could even have no native DOM elements at all and let the generic components deal with the templating and user interactions.

scope somehow events or stores visibility for siblings components

This is where the scoping happens. Most components don't know about the store and this component should (mostly) use one namespaced store module with a limited set of getters and actions applied with the provided Vuex binding helpers.

Generic/presentational components

These should receive their data from props, make changes on their own local data, and emit simple events. Most of the time, they should not know a Vuex store exists at all.

They could also be called containers as their sole responsibility could be to dispatch to other UI components.


Sibling communication

So, after all this, how should we communicate between two sibling components?

It's easier to understand with an example: say we have an input box and its data should be shared across the app (siblings at different places in the tree) and persisted with a backend.

❌ Mixing concerns

Starting with the worst case scenario, our component would mix presentation and business logic.

// MyInput.vue
<template>
    <div class="my-input">
        <label>Data</label>
        <input type="text"
            :value="value" 
            :input="onChange($event.target.value)">
    </div>
</template>
<script>
    import axios from 'axios';

    export default {
        data() {
            return {
                value: "",
            };
        },
        mounted() {
            this.$root.$on('sync', data => {
                this.value = data.myServerValue;
            });
        },
        methods: {
            onChange(value) {
                this.value = value;
                axios.post('http://example.com/api/update', {
                        myServerValue: value
                    });
            }
        }
    }
</script>

While it might look fine for a simple app, it comes with a lot of drawbacks:

  • Explicitly uses the global axios instance
  • Hard-coded API inside the UI
  • Tightly coupled to the root component (event bus pattern)
  • Harder to do unit tests

✅ Separation of concerns

To separate these two concerns, we should wrap our component in an app specific container and keep the presentation logic into our generic input component.

With the following pattern, we can:

  • Easily test each concern with unit tests
  • Change the API without impacting components at all
  • Configure HTTP communications however you'd like (axios, fetch, adding middlewares, tests, etc)
  • Reuse the input component anywhere (reduced coupling)
  • React to state changes from anywhere in the app through the global store bindings
  • etc.

Our input component is now reusable and doesn't know about the backend nor the siblings.

// MyInput.vue
// the template is the same as above
<script>
    export default {
        props: {
            initial: {
                type: String,
                default: ""
            }
        },
        data() {
            return {
                value: this.initial,
            };
        },
        methods: {
            onChange(value) {
                this.value = value;
                this.$emit('change', value);
            }
        }
    }
</script>

Our app specific container can now be the bridge between the business logic and the presentation communication.

// MyAppCard.vue
<template>
    <div class="container">
        <card-body>
            <my-input :initial="serverValue" @change="updateState"></my-input>
            <my-input :initial="otherValue" @change="updateState"></my-input>

        </card-body>
        <card-footer>
            <my-button :disabled="!serverValue || !otherValue"
                       @click="saveState"></my-button>
        </card-footer>
    </div>
</template>
<script>
    import { mapGetters, mapActions } from 'vuex';
    import { NS, ACTIONS, GETTERS } from '@/store/modules/api';
    import { MyButton, MyInput } from './components';

    export default {
        components: {
            MyInput,
            MyButton,
        },
        computed: mapGetters(NS, [
            GETTERS.serverValue,
            GETTERS.otherValue,
        ]),
        methods: mapActions(NS, [
            ACTIONS.updateState,
            ACTIONS.saveState,
        ])
    }
</script>

Since the Vuex store actions deal with the backend communication, our container here doesn't need to know about axios and the backend.

Matrona answered 7/4, 2018 at 1:34 Comment(14)
Agree with comment about methods being "the same coupling that of using props"Smallscale
I like this answer. But could you please elaborate on Event Bus and "Be careful:" note? Maybe you can give some example, I don't understand how components could become binded twice.Swaim
How do you communicate between parent component and grand child component for example form validation. Where parent component is a page, child is form, and grand child is input form element?Bristle
@Swaim I created a simple example that shows a leak when the listeners are not removed properly, like every examples in this thread.Matrona
@LordZed It really depends, but from my understanding of your situation, it looks like a design problem. Vue should be used mostly for presentation logic. Form validation should be done elsewhere, like in the vanilla JS API interface, that a Vuex action would call with the data from the form.Matrona
@EmileBergeron do you want to say that now all of sudden we don't need client side validation of input data?Bristle
@LordZed That's not what I've said.Matrona
Stop conflating Vuex with business logic! Vuex is for state management. It can interact with a business logic layer but it is not for business logic. And calling a child method is not an anti-pattern. I've seen so many jr devs contorting to the dogma of props/events by writing some truly convoluted code when a simple method call would solve the problem.Repugnance
@Repugnance I've seen the opposite, where everything was put inside Vuex, so when I say business logic, I mean, limit what's in Vuex to necessary global states and rules (through actions) which is often closer to business logic than it is to UI. It doesn't mean to put all the actual logic inside Vuex, but that the shared state would be easier to manage inside Vuex. The actual logic could be completely separated/isolated elsewhere, but that's just guidelines. If you're making a quick app, anything could live inside a couple components locally and it would be fine!Matrona
Right, that's my point though. There is a notion among the community that Vue/React go hand in hand with Vuex/Redux and it leads to every other project with bloated stores that do more business logic than state management. I agree that in smaller projects, the lines can and do blur, but the association of centralized store and business logic is wrong. Most any project will have business logic. Not all projects will have centralized state.Repugnance
@Repugnance I agree, the use of centralized store has seen a decline since this answer was written. I've been using React mostly and we have completely removed Redux from our toolset and we instead use a mix of Apollo GraphQL client (which manages a global store automagically) and context, which would be equivalent to provide/inject in vue I believe, which isn't even addressed (yet) in my answer since it was relatively new when I answered.Matrona
@Repugnance your comment prompted me to update my answer. I've removed most references to business logic as it's an entirely different concern than communication channels and state management and I introduced more alternatives and nuance to invite readers to make their own mind about which solution to use. I've also toned down the warning about refs and child methods, as it's a totally valid way in some situation.Matrona
Pinia is also a great alternative now, less boilerplate (and recommended by the core team).Nordrheinwestfalen
Thanks for the info @kissu, I've added a note in the centralized store section of my answer. I'm not using vue anymore since 2018 so I've lost touch with on-going development.Matrona
H
11

Okay, we can communicate between siblings via the parent using v-on events.

Parent
 |- List of items // Sibling 1 - "List"
 |- Details of selected item // Sibling 2 - "Details"

Let's assume that we want update Details component when we click some element in List.


In Parent:

Template:

<list v-model="listModel"
      v-on:select-item="setSelectedItem" 
></list> 
<details v-model="selectedModel"></details>

Here:

  • v-on:select-item it's an event, that will be called in List component (see below);
  • setSelectedItem it's a Parent's method to update selectedModel;

JavaScript:

//...
data () {
  return {
    listModel: ['a', 'b']
    selectedModel: null
  }
},
methods: {
  setSelectedItem (item) {
    this.selectedModel = item // Here we change the Detail's model
  },
}
//...

In List:

Template:

<ul>
  <li v-for="i in list" 
      :value="i"
      @click="select(i, $event)">
        <span v-text="i"></span>
  </li>
</ul>

JavaScript:

//...
data () {
  return {
    selected: null
  }
},
props: {
  list: {
    type: Array,
    required: true
  }
},
methods: {
  select (item) {
    this.selected = item
    this.$emit('select-item', item) // Here we call the event we waiting for in "Parent"
  },
}
//...

Here:

  • this.$emit('select-item', item) will send an item via select-item directly in the parent. And the parent will send it to the Details view.
Hodgkinson answered 10/8, 2016 at 14:29 Comment(0)
P
8

How to handle communication between siblings depends on the situation. But first I want to emphasize that the global event bus approach is going away in Vue.js 3. See this RFC. Hence this answer.

Lowest Common Ancestor Pattern (or “LCA”)

For most cases, I recommend using the lowest common ancestor pattern (also known as “data down, events up”). This pattern is easy to read, implement, test, and debug. It also creates an elegant, simple data flow.

In essence, this means if two components need to communicate, put their shared state in the closest component that both share as an ancestor. Pass data from parent component to child component via props, and pass information from child to parent by emitting an event (example code below).

For example, one might have an email app: the address component needs to communicate data to the message body component (perhaps for pre-populating "Hello <name>"), so they use their closest shared ancestor (perhaps an email form component) to hold the addressee data.

LCA can be annoying if events and props need to pass through many "middlemen" components.

For more detail, I refer colleagues to this excellent blog post. (Ignore the fact that its examples use Ember, its concepts are applicable to many frameworks).

Data Container Pattern (e.g., Vuex)

For complex cases or situations where parent–child communication would involve too many middlemen, use Vuex or an equivalent data container technology.

Use namespaced modules when a single store becomes too complicated or disorganized. For example, it might be reasonable to create a separate namespace for a complex collection of components with many interconnections, such as a complex calendar.

Publish/Subscribe (Event Bus) Pattern

If the event bus (i.e. publish/subscribe) pattern makes more sense for your app (from an architecture standpoint), or you need to remove Vue.js's global event bus from an existing Vue.js app, the Vue.js core team now recommends using a third party library such as mitt. (See the RFC referenced in paragraph 1.).

Miscellaneous

Here's a small (perhaps overly simplistic) example of an LCA solution for sibling-to-sibling communication. This is a game called whack-a-mole.

In this game the player gets points when they "whack" a mole, which causes it to hide and then another mole appears in a random spot. To build this app, which contains "mole" components, one might think , “mole component N should tell mole component Y to appear after it is whacked”. But Vue.js discourages this method of component communication, since Vue.js apps (and html) are effectively tree data structures.

This is probably a good thing. A large/complex app, where nodes communicated to each-other without any centralized manager, might be very difficult to debug. Additionally, components that use LCA tend to exhibit low coupling and high reusability.

In this example, the game manager component passes mole visibility as a prop to mole child components. When a visible mole is "whacked" (clicked), it emits an event. The game manager component (the common ancenstor) receives the event and modifies its state. Vue.js automatically updates the props, so all of the mole components receive new visibility data.

Vue.component('whack-a-mole', {
  data() {
    return {
      stateOfMoles: [true, false, false],
      points: 0
    }
  },
  template: `<div>WHACK - A - MOLE!<br/>
    <a-mole :has-mole="stateOfMoles[0]" v-on:moleMashed="moleClicked(0)"/>
    <a-mole :has-mole="stateOfMoles[1]"  v-on:moleMashed="moleClicked(1)"/>
    <a-mole :has-mole="stateOfMoles[2]" v-on:moleMashed="moleClicked(2)"/>
    <p>Score: {{points}}</p>
</div>`,
  methods: {
    moleClicked(n) {
      if(this.stateOfMoles[n]) {
         this.points++;
         this.stateOfMoles[n] = false;
         this.stateOfMoles[Math.floor(Math.random() * 3)] = true;
      }
    }
  }
})

Vue.component('a-mole', {
  props: ['hasMole'],
  template: `<button @click="$emit('moleMashed')">
      <span class="mole-button" v-if="hasMole">🐿</span><span class="mole-button" v-if="!hasMole">🕳</span>
    </button>`
})

var app = new Vue({
  el: '#app',
  data() {
    return { name: 'Vue' }
  }
})
.mole-button {
  font-size: 2em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
  <whack-a-mole />
</div>
Pedropedrotti answered 27/3, 2020 at 22:43 Comment(0)
C
7

What I usually do if I want to "hack" the normal patterns of communication in Vue.js, specially now that .sync is deprecated, is to create a simple EventEmitter that handles communication between components. From one of my latest projects:

import {EventEmitter} from 'events'

var Transmitter = Object.assign({}, EventEmitter.prototype, { /* ... */ })

With this Transmitter object you can then do, in any component:

import Transmitter from './Transmitter'

var ComponentOne = Vue.extend({
  methods: {
    transmit: Transmitter.emit('update')
  }
})

And to create a "receiving" component:

import Transmitter from './Transmitter'

var ComponentTwo = Vue.extend({
  ready: function () {
    Transmitter.on('update', this.doThingOnUpdate)
  }
})

Again, this is for really specific uses. Don't base your whole application on this pattern, use something like Vuex instead.

Clayborn answered 28/7, 2016 at 3:47 Comment(5)
I'm already use vuex, but again, should I create vuex's store for each minor communication?Hodgkinson
It's hard for me to say with this amount of information, but I'd say that if you're already using vuex yes, go for it. Use it.Clayborn
Actually I would disagree that we need to use vuex for each minor communication...Vintager
No, of course not, it all depends on the context. Actually my answer moves away from vuex. On the other hand, I've found that the more you use vuex and the concept of a central state object, the less I'm relying on communication between objects. But yes, agree, it all depends.Clayborn
I would say using Vuex is a more orderly and resilient strategy in the long run. Do not use Vuex for any state constrained to one component, use Vuex for any state that leaks between components, unless there is an obvious reason not to (for example if a two-way binding between parent and child component will suffice). Also don't create one new vuex module for every single thing, you can have a "root" module in store apt for app level state.Emmy
R
0

In my case i have a table with editable cells. I only want one cell to be editable at a time as the user clicks from one to another to edit the contents. The solution is to use parent-child (props) and child-parent (event). In the example below i'm looping over a dataset of 'rows' and using the rowIndex and cellIndex to create a unique (coordinate) identifier for each cell. When a cell is clicked an event is fired from the child element up to the parent telling the parent which coordinate has been clicked. The parent then sets the selectedCoord and passes this back down to the child components. So each child component knows its own coordinate and the selected coordinate. It can then decide whether to make itself editable or not.

<!-- PARENT COMPONENT -->
<template>
<table>
    <tr v-for="(row, rowIndex) in rows">
        <editable-cell
            v-for="(cell, cellIndex) in row"
            :key="cellIndex"
            :cell-content="cell"
            :coords="rowIndex+'-'+cellIndex"
            :selected-coords="selectedCoords"
            @select-coords="selectCoords"
        ></editable-cell>
    </tr>
</table>
</template>
<script>
export default {
    name: 'TableComponent'
    data() {
        return {
            selectedCoords: '',
        }
    },
    methods: {
        selectCoords(coords) {
            this.selectedCoords = coords;
        },
    },
</script>

<!-- CHILD COMPONENT -->
<template>
    <td @click="toggleSelect">
        <input v-if="coords===selectedCoords" type="text" :value="cellContent" />
        <span v-else>{{ cellContent }}</span>
    </td>
</template>
<script>
export default {
    name: 'EditableCell',
    props: {
        cellContent: {
            required: true
        },
        coords: {
            type: String,
            required: true
        },
        selectedCoords: {
            type: String,
            required: true
        },
    },
    methods: {
        toggleSelect() {
            const arg = (this.coords === this.selectedCoords) ? '' : this.coords;
            this.$emit('select-coords', arg);
        },
    }
};
</script>
Rochellrochella answered 26/3, 2021 at 23:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.