当前位置:网站首页>24.全局事件总线
24.全局事件总线
2022-07-24 07:18:00 【风落不归处】
1.全局事件总线
(EventBus)可以称之为全局事件总线,当两个组件属于不同的组件分支,或者两个组件没有任何联系的时候,不想使用Vuex这样的库进行数据通信,就可以通过事件总线来进行通信。Vue事件总线就像所有组件的事件中心,在组件中,我们可以使用$emit,$on,$off分别来分发、监听、取消监听事件。
2.Vue事件总线的使用
如下代码注册的事件总线实际是借助Vue实例对象本身
1.创建全局事件总线,在main.js中创建
main.js文件
//引入Vue
import Vue from 'vue'
//引入App
import App from './App.vue'
//关闭Vue的生产提示
Vue.config.productionTip = false
//创建vm
new Vue({
el:'#app',
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this //安装全局事件总线
},
})下面通过在main.js中注册的$Bus总线实现兄弟组件的数据通信(本质是借助vm本身:vue实例,来实现全局事件总线,因为在vc中可以访问到vm中的原型对象数据)
实现Student组件向School组件中发送数据进行通信
School组件
<template>
<div class="school">
<h2>School接收的学生名字:{
{ studentName }}</h2>
</div>
</template>
<script>
export default {
name: "School",
data() {
return {
studentName: "",
}
},
methods: {
},
mounted() {
this.$bus.$on("getStudentName",studentName => {
console.log("School接收的studentName:",studentName)
this.studentName = studentName
})
},
beforeDestroy() {
this.$bus.$off("getStudentName")
}
}
</script>Student组件
<template>
<div class="student">
<h2>学生名字:{
{ studentName }}</h2>
<button @click="sendStudentName">发送学生姓名给School组件</button>
</div>
</template>
<script>
export default {
name: "Student",
data() {
return {
studentName: "张三"
}
},
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>注意:最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件
边栏推荐
- Vulnhub DC1
- 给一个字符串 ① 请统计出其中每一个字母出现的次数② 请打印出字母次数最多的那一对
- The goal you specified requires a project to execute but there is no POM in this directory
- The goal you specified requires a project to execute but there is no POM in this directory
- In the era of e-commerce, what should enterprises do in the transformation of social e-commerce?
- Variables and data types (03)
- Three implementation methods of single sign on
- File upload and download demo
- 安全工具之hackingtool
- C language start
猜你喜欢

编译与调试(gcc,g++,gdb)

InjectFix原理学习(实现修复加法的热更)

cookie_ session

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

项目上线就炸,这谁受得了

全国职业院校技能大赛网络安全B模块 Windows操作系统渗透测试

sqli-labs简单安装

Blockbuster live broadcast | orb-slam3 series code explanation map points (topic 2)

C language from entry to soil (I)

Stm32h750vbt6 drives programmable gain amplifier module pga113 -- Hal Library Based on cubemx
随机推荐
【行测】图形找规律类题目
A great hymn
[leetcode] 11. Container with the most water - go language solution
numpy.cumsum
开发系统选择路线
Oauth2==SSO三种协议。Oauth2四种模式
Traditional e-commerce dividends disappear, how to enter the new social e-commerce?
[leetcode] 444. Sequence reconstruction
周杰伦直播超654万人观看,总互动量破4.5亿,助力快手再破纪录
Kali安装pip以及pip换源
传统电商红利消失,怎么进军新型社交电商?
【Tips】创建版本控制项目的简单方法
UNI-APP_小程序或h5页面背景音乐的播放与暂停
学习笔记-分布式事务理论
Unity中使用深度和法线纹理
Simple installation of sqli Labs
记账APP:小哈记账1——欢迎页的制作
[PTA] group programming ladder competition - Summary of exercises L3 (incomplete)
解压主播狂揽4000w+播放,快手美食赛道又添新风向?
baddy:核心函数入口