How should the two components of vue communicate when they are neither parent-child nor sibling?

I want to click on component B to add data to component A
component A

<template>
   <div>
     <ul>
       <li v-for="(item, index) in list" :key=index>{{item}}</li>
     </ul>
     <router-link to="/b">B</router-link>
   </div>
</template>

<script>
export default {
  name: "A",
  data () {
    return {
      list: ["a", "b", "c"]
    }
  }
}
</script>

<style scoped>
</style>

component B

<template>
  <div>
    <input type="text" v-model="msg">
    <button @click="add"></button>
  </div>
</template>

<script>
export default {
  name: "B",
  data () {
    return {
      msg: ""
    }
  },
  methods: {
    add () {
    }
  }
}
</script>

<style scoped>
</style>
Jul.29,2021

A component
listens to the specified message componentsMessage of the root component $root . This componentsMessage can be customized. As long as the listening / distributing message string is the same, you can receive

.
data() {
  return {
    list: ['a', 'b', 'c']
  }
},
created() {
  this.$root.$on('componentsMessage', v => {
    this.list.push(v)
  })
},

B component
you can distribute messages through the root instance $root in any component, and you can receive

as long as you are listening.
methods: {
  add() {
    this.$root.$emit('componentsMessage', 'd')
  },

Bus mechanism or Vuex


you can consider using vuex

Menu