当前位置:网站首页>24. Global event bus
24. Global event bus
2022-07-24 07:25:00 【Where the wind falls】
1. Global event bus
(EventBus) It can be called global event bus , When two components belong to different component branches , Or when there is no connection between the two components , Do not want to use Vuex Such a library for data communication , You can communicate through the event bus .Vue The event bus is like the event center of all components , In components , We can use $emit,$on,$off Distribute separately 、 monitor 、 Cancel listening Events .
2.Vue The use of event bus
The event bus registered by the following code actually uses Vue Instance object itself
1. Create a global event bus , stay main.js Created in
main.js file
// introduce Vue
import Vue from 'vue'
// introduce App
import App from './App.vue'
// close Vue Production tips for
Vue.config.productionTip = false
// establish vm
new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this // Install the global event bus
},
})The following is passed in main.js Registered in $Bus The bus realizes the data communication of brother components ( The essence is to rely on vm In itself :vue example , To implement the global event bus , Because in vc You can access vm Prototype object data in )
Realization Student Components to School Send data in the component for communication
School Components
<template>
<div class="school">
<h2>School Received student name :{
{ studentName }}</h2>
</div>
</template>
<script>
export default {
name: "School",
data() {
return {
studentName: "",
}
},
methods: {
},
mounted() {
this.$bus.$on("getStudentName",studentName => {
console.log("School Received studentName:",studentName)
this.studentName = studentName
})
},
beforeDestroy() {
this.$bus.$off("getStudentName")
}
}
</script>Student Components
<template>
<div class="student">
<h2> Student name :{
{ studentName }}</h2>
<button @click="sendStudentName"> Send student name to School Components </button>
</div>
</template>
<script>
export default {
name: "Student",
data() {
return {
studentName: " Zhang San "
}
},
methods: {
sendStudentName() {
this.$bus.$emit("getStudentName",this.studentName)
}
}
}
</script>App
<template>
<div class="app">
<school />
<student />
</div>
</template>
<script>
import School from "./components/School.vue";
import Student from "./components/Student.vue";
export default {
name: "App",
components: {
School,
Student
},
data() {
return {
}
},
methods: {
}
}
</script>Be careful : Best in beforeDestroy In the hook , use $off Unbind the events used by the current component
边栏推荐
- Basic syntax of MySQL DDL and DML and DQL
- Injectfix principle learning (to realize the heat of repair addition)
- 野指针,空指针,失效指针
- Using depth and normal textures in unity
- [sequential logic circuit] - register
- FPGA实现AXI4总线的读写
- oracle中有A,B连个表,这两个表需要第三个表C关联,那怎么将A表中的字段MJ1更新为B表中MJ2的值
- numpy.concatenate
- Basic syntax of MySQL DDL and DML and DQL
- 单场GMV翻了100倍,冷门品牌崛起背后的“通用法则”是什么?
猜你喜欢

My creation anniversary

FPGA实现AXI4总线的读写

MITRE ATT&CK超详细学习笔记-02(大量案例)

Variables and data types (04) end

Riotboard development board series notes (IX) -- buildreoot porting matchbox

C language from introduction to soil -- super detailed summary of operators

Laplace distribution

RIoTBoard开发板系列笔记(九)—— buildroot 移植MatchBox

2D目标检测综述之学习策略篇(终章)

QoS quality of service three DiffServ Model message marking and PHB
随机推荐
The goal you specified requires a project to execute but there is no POM in this directory
17. What is the situation of using ArrayList or LinkedList?
给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
[leetcode] 11. Container with the most water - go language solution
Jackson 解析 JSON 详细教程
【LeetCode-简单】20. 有效的括号 - 栈
Feature Selective Anchor-Free Module for Single-Shot Object Detection
From the perspective of CIA, common network attacks (blasting, PE, traffic attacks)
[sequential logic circuit] - register
解压主播狂揽4000w+播放,快手美食赛道又添新风向?
Three implementation methods of single sign on
UNI-APP_小程序或h5页面背景音乐的播放与暂停
Wild pointer, null pointer, invalid pointer
Give a string ① please count the number of times each letter appears ② please print the pair with the most letters
csdn,是时候说再见!
PHP link log scheme
研究会2022.07.22--对比学习
Kali installing PIP and pip source changing
libsvm 使用参数的基础知识笔记(1)
B. Also Try Minecraft