VDone Demo VDone Demo
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)

Nikolay Tuzov

Backend Developer
Home
  • Articles

    • JavaScript
  • Study Notes

    • JavaScript Tutorial
    • Professional JavaScript
    • ES6 Tutorial
    • Vue
    • React
    • TypeScript: Build Axios from Scratch
    • Git
    • TypeScript
    • JS Design Patterns
  • HTML
  • CSS
  • Technical Docs
  • GitHub Tips
  • Node.js
  • Blog Setup
  • Learning
  • Interviews
  • Miscellaneous
  • Practical Tips
  • Friends
About
Bookmarks
  • Categories
  • Tags
  • Archives
GitHub (opens new window)
  • 基础

  • 组件

    • Component Usage Details
    • Passing Data from Parent to Child Components
    • Child Component Emitting Events and Values to Parent
    • Prop Validation and Non-Prop Attributes
    • Custom Events
    • Sibling Component Communication
    • Non-Parent-Child Component Communication
    • Parent Calling Child Component Methods with Arguments
    • Slots
    • Dynamic Components and v-once Directive
    • Vue Parent-Child Component Lifecycle Order
  • 过渡&动画

  • 可复用性&组合

  • 工具

  • 规模化

  • Vuex

  • 其他

  • 《Vue》笔记
  • 组件
xugaoyi
2020-02-15

Non-Parent-Child Component Communication

# Non-Parent-Child Component Communication

When components are deeply nested, passing data between non-parent-child components becomes complex. Besides using Vuex (opens new window) for state management, you can also use a Bus (also known as an event bus / publish-subscribe pattern / observer pattern) to achieve communication between non-parent-child components.

<div id="root">
    <child1 content="Component 1: Click me to emit value"></child1>
    <child2 content="Component 2"></child2>
</div>

<script type="text/javascript">
	Vue.prototype.bus = new Vue()
	// Every Vue prototype will have a bus property pointing to the same Vue instance

	Vue.component('child1', {
		props: {
			content: String
		},
		template: '<button @click="handleClick">{{content}}</button>',
		methods: {
			handleClick(){
				this.bus.$emit('change', 'I came from component 1~') // trigger change event and emit value
			}
		}
	})

	Vue.component('child2', {
		data() {
			return {
				childVal: ''
			}
		},
		props: {
			content: String,
		},
		template: '<button>{{content}} + {{childVal}}</button>',
		mounted() {
			this.bus.$on('change', (msg) => { // listen for change event and receive value
				this.childVal = msg
			})
		}
	})

	var vm = new Vue({
		el: '#root'
	})
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

In the code above, a bus property is bound to the Vue prototype, pointing to a Vue instance. After this, every Vue instance will have a bus property.

This method is not limited to sibling components -- it works between components of any relationship.

See the Pen 非父子组件间传值2(Bus /总线/发布订阅模式/观察者模式) by xugaoyi (@xugaoyi) on CodePen.

Edit (opens new window)
#Vue
Last Updated: 2026/03/21, 12:14:36
Sibling Component Communication
Parent Calling Child Component Methods with Arguments

← Sibling Component Communication Parent Calling Child Component Methods with Arguments→

Recent Updates
01
How I Discovered Disposable Email — A True Story
06-12
02
Animations in Grid Layout
09-15
03
Renaming a Git Branch
08-11
More Articles >
Theme by VDone | Copyright © 2026-2026 Nikolay Tuzov | MIT License | Telegram
  • Auto
  • Light Mode
  • Dark Mode
  • Reading Mode