search
HomeWeb Front-endJS TutorialHow event bus non-parent-child components communicate with each other

This time I will show you how event bus non-parent and child components communicate with each other. What are the precautions for event bus non-parent and child components to communicate with each other? Here is a practical case, let’s take a look.

Sometimes components that are not in a parent-child relationship also need to communicate. In a simple scenario, use an empty Vue instance as the central Event bus:

var bus = new Vue()
// 触发组件 A 中的事件
bus.$emit('id-selected', 1)
// 在组件 B 创建的钩子中监听事件
bus.$on('id-selected', function (id) {
 // ...
})

In more complex cases, you should consider using a dedicated state management mode. That is using vuex

eventBus is a communication intermediary between components that act as brothers.

Code example:

nbsp;html>


<title>eventBus</title>
<script></script>


<p>
</p><h1 id="todo-app">todo app</h1>
<new-todo></new-todo>
<todo-list></todo-list>

<script>
var eventHub = new Vue( {
data(){
return{
todos:[&#39;A&#39;,&#39;B&#39;,&#39;C&#39;]
}
},
created:function () {
this.$on(&#39;add&#39;, this.addTodo)
this.$on(&#39;delete&#39;, this.deleteTodo)
},
beforeDestroy:function () {
this.$off(&#39;add&#39;, this.addTodo)
this.$off(&#39;delete&#39;, this.deleteTodo)
},
methods: {
addTodo: function (newTodo) {
this.todos.push(newTodo)
},
deleteTodo: function (i) {
this.todos.splice(i,1)
}
}
})
var newTodo = {
template:`<p><input type="text" autofocus v-model="newtodo"/><button @click="add">add`,
data(){
return{
newtodo:&#39;&#39;
}
},
methods:{
add:function(){
eventHub.$emit(&#39;add&#39;, this.newtodo)
this.newtodo = &#39;&#39;
}
}
}
var todoList = {
template:`<ul><li v-for="(index,item) in items">{{item}} \
     <button @click="rm(index)">X \
     `,
     data(){
     return{
     items:eventHub.todos
     }
     },
     methods:{
     rm:function(i){
     eventHub.$emit(&#39;delete&#39;,i)
     }
     }
}
var app= new Vue({
el:&#39;#todo-app&#39;,
components:{
newTodo:newTodo,
todoList:todoList
}
})
</script>

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the PHP Chinese website!

Recommended reading:

How does JS detect scripts in the browser

Popup popup box binding add data event ( Detailed explanation of steps)

The above is the detailed content of How event bus non-parent-child components communicate with each other. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP8.0中的事件处理库:EventPHP8.0中的事件处理库:EventMay 14, 2023 pm 05:40 PM

PHP8.0中的事件处理库:Event随着互联网的不断发展,PHP作为一门流行的后台编程语言,被广泛应用于各种Web应用程序的开发中。在这个过程中,事件驱动机制成为了非常重要的一环。PHP8.0中的事件处理库Event将为我们提供一个更加高效和灵活的事件处理方式。什么是事件处理在Web应用程序的开发中,事件处理是一个非常重要的概念。事件可以是任何一种用户行

Steam Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content WarningSteam Summer Sale - Valve teases 95% off AAA games, confirms discounts for viral games Palworld and Content WarningJun 26, 2024 pm 03:40 PM

Steam's Summer Sale has previously played host to some of the best game discounts, and this year seems to be stacking up for another home run by Valve. A trailer (watch below) teasing some of the Steam Summer Sale discounted games was just released i

Python之Pygame的Event事件模块怎么使用Python之Pygame的Event事件模块怎么使用May 18, 2023 am 11:58 AM

Pygame的Event事件模块事件(Event)是Pygame的重要模块之一,它是构建整个游戏程序的核心,比如常用的鼠标点击、键盘敲击、游戏窗口移动、调整窗口大小、触发特定的情节、退出游戏等,这些都可以看做是“事件”。事件类型Pygame定义了一个专门用来处理事件的结构,即事件队列,该结构遵循遵循队列“先到先处理”的基本原则,通过事件队列,我们可以有序的、逐一的处理用户的操作(触发事件)。下述表格列出了Pygame中常用的游戏事件:名称说明QUIT用户按下窗口的关闭按钮ATIVEEVENTPy

Steam Summer Sale trailer teases 95% off AAA game deals, confirms price cuts for Palworld, Stellaris, Content WarningSteam Summer Sale trailer teases 95% off AAA game deals, confirms price cuts for Palworld, Stellaris, Content WarningJun 26, 2024 am 06:30 AM

Steam's Summer Sale has previously played host to some of the best game discounts, and this year seems to be stacking up for another home run by Valve. A trailer (watch below) teasing some of the Steam Summer Sale discounted games was just released i

Tesla sends out Robotaxi invitations for October 10 autonomous driving demo event in LATesla sends out Robotaxi invitations for October 10 autonomous driving demo event in LASep 27, 2024 am 06:20 AM

It was initially expected that Tesla would unveil its previously leaked Robotaxi back in August of this year, but CEO Elon Musk postponed the event, citing aesthetic changes to the front of the robotaxi and additional time needed for a few last-minut

在JavaScript中,当浏览器窗口调整大小时,这是哪个事件?在JavaScript中,当浏览器窗口调整大小时,这是哪个事件?Sep 05, 2023 am 11:25 AM

使用window.outerWidth和window.outerHeight事件在JavaScript中获取窗口大小,当浏览器调整大小时。示例您可以尝试运行以下代码来使用事件检查浏览器窗口大小&minus;&lt;!DOCTYPEhtml&gt;&lt;html&gt;&nbsp;&nbsp;&lt;head&gt;&nbsp;&nbsp;&nbsp;&lt;script&gt;&am

评估Win10:韩博士的看法评估Win10:韩博士的看法Jan 05, 2024 pm 12:11 PM

win10是一个非常好用的系统,但是有时候这个系统也是会出问题的,于是很多对电脑不精通的用户就会选择重装系统来彻底解决这个问题,他们通常会选择韩博士,于是就想要了解一下这个软件怎么样呢,那就一起来文中看看吧。韩博士win10怎么样答:这是一个还不错的win10系统。这款软件可以做到没有任何基础的用户,在不需要光驱和U盘的情况下,通过简单的操作和流程完成对系统的重装。软件同时提供了系统的备份镜像系统和还原镜像系统这两个非常重要的系统重装工具。同时软件中还有非常多的其他功能,比如一键重装系统、u盘重

Tesla Robotaxi reveal to go ahead on October 10 as invitations go out to select shareholdersTesla Robotaxi reveal to go ahead on October 10 as invitations go out to select shareholdersSep 26, 2024 pm 06:06 PM

It was initially expected that Tesla would unveil its previously leaked Robotaxi back in August of this year, but CEO Elon Musk postponed the event, citing aesthetic changes to the front of the robotaxi and additional time needed for a few last-minut

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool