Vue29 自定义事件及消息总线
1 简介
组件自定义事件是一种组件间的通信方式,方向是 子组件====>父组件。
使用场景:A是父组件,B是子组件,如果要把B的数据传给A,可以使用props加回调函数实现或者自定义事件实现。
2 自定义组件
主要分为两个步骤:1绑定事件回调函数 2触发事件
2.1 绑定事件回调函数
它有两种写法
1)在组件标签上直接绑定
<StudentComp v-on:student="getStudentName" ></StudentComp>
简写
<StudentComp @student="getStudentName" ></StudentComp>
methods: {
getStudentName(stname){
console.log(this) //SchoolComp的vc对象
console.log('在SchoolComp中事件student的回调函数getStudentName被调用:','@',stname)
}
}
2)ref属性加上$on('事件名',回调方法)
<StudentComp ref="stud" ></StudentComp>
手动去绑定事件(这里选择挂载后去绑定),这种方式更加灵活,可以选择在什么时候去绑定
mounted() {
this.$refs.stud.$on('student',this.getStudentName)
},
2.2 触发事件$emit('事件名','参数1','参数2',...)
<button v-on:click="showName">点击</button>
methods: {
showName(){
console.log('在StudentComp中触发student事件')
this.$emit('student',this.stname)
}
},
3 示例
SchoolComp是StudentComp的父组件,现在StudentComp向SchoolComp传一个值stname
3.1 第一种绑定方式
1)main.js
import Vue from 'vue'
import App from './App.vue' Vue.config.productionTip = false new Vue({
render: h => h(App),
}).$mount('#app')
2) App.vue
<template>
<div>
<SchoolComp></SchoolComp> </div>
</template> <script> import SchoolComp from './components/SchoolComp' export default {
name:'App',
components:{
SchoolComp
}
} </script>
3)SchoolComp.vue
<template>
<div>
<h1 >{{schoolname}}</h1>
<StudentComp v-on:student="getStudentName" ></StudentComp>
</div>
</template> <script>
import StudentComp from './StudentComp' export default {
name:'SchoolComp',
data(){
return {
schoolname:'实验小学1',
age:18
}
},
components:{
StudentComp
},
methods: {
getStudentName(stname){
console.log(this) //SchoolComp的vc对象
console.log('在SchoolComp中事件student的回调函数getStudentName被调用:','@',stname)
}
},
}
</script> <style> </style>
4)StudentComp.vue
<template>
<div>
<h1>{{stname}}</h1>
<button v-on:click="showName">点击</button> </div> </template> <script> export default {
name:'StudentComp',
data(){
return {
stname:'小新'
}
},
methods: {
showName(){
console.log('在StudentComp中触发student事件')
this.$emit('student',this.stname)
}
},
}
</script>
5) 效果
点击按钮,事件触发,SchoolComp中的回调被调用,且接收到了stname

3.2 第二种绑定方式
修改下SchoolComp.vue
<template>
<div>
<h1 >{{schoolname}}</h1>
<StudentComp ref="stud" ></StudentComp>
</div>
</template> <script>
import StudentComp from './StudentComp' export default {
name:'SchoolComp',
data(){
return {
schoolname:'实验小学1',
age:18
}
},
components:{
StudentComp
},
methods: {
getStudentName(stname){
console.log(this) //SchoolComp的vc对象
console.log('在SchoolComp中事件student的回调函数getStudentName被调用:','@',stname)
}
},
mounted() {
this.$refs.stud.$on('student',this.getStudentName)
},
}
</script> <style> </style>
4 解绑
4.1 语法
1)解除单个事件绑定
$off('事件名')
2)解除多个事件绑定
$off(['事件名1','事件名2'])
3) 解除所有事件绑定
$off()
4.2 示例
在StudentComp.vue中解绑,解除绑定后,事件不会被触发,回调函数不会被调用
<template>
<div>
<h1>{{stname}}</h1>
<button v-on:click="showName">点击</button>
<button v-on:click="cancelBind">解除绑定</button> </div> </template> <script> export default {
name:'StudentComp',
data(){
return {
stname:'小新'
}
},
methods: {
showName(){
console.log('在StudentComp中去触发student事件')
this.$emit('student',this.stname)
},
cancelBind(){
this.$off('student')
}
},
}
</script>
5 组件标签上使用原生事件
@原生事件名.native='方法'
如
<StudentComp @click.native="cli" ></StudentComp>
6 自定义事件注意事项
通过this.$refs.stud.$on('student',this.getStudentName)这种方式绑定时,回调函数写在methods里面,回调函数里面的this就是当前组件实例对象,也就是SchoolComp实例
如果把回调函数直接写在on里面,this就是触发事件的组件实例,也就是StudentComp实例
mounted() {
this.$refs.stud.$on('student',function getStudentName(stname){
console.log(this) //Student的vc对象
console.log(stname)
})
}
要想获得当前组件实例,需要写成箭头函数
mounted() {
this.$refs.stud.$on('student', (stname)=>{
console.log(this) //SchoolComp的vc对象
console.log(stname)
})
},
7 自定义事件小结
自定义事件实际上就是在一个组件中为另一个组件绑定一个事件并定义回调函数,在绑定事件的那个组件中触发事件

8 事件总线
8.1 简介
在上面,我们实现了子传数据到父,而通过props可以实现父传数据到子。
那么兄弟之间如何通信呢,爷爷和孙子的通信呢?
可以利用事件总线来实现任意两个组件之间的通信,它就是利用自定义事件来实现的,使用一种非常巧妙的用法。
eventBus 又称为事件总线。在 Vue 中可使用 eventBus 来作为沟通桥梁的概念,就像是所有组件共用相同的事件中心,可向该中心注册发送事件或接收事件,所以组件都可以上下平行地通知其他组件。
8.2 图示
事件总线就是通过一个处处可见的第三者组件,来实现任意两个组件之间的通信

9 事件总线的使用
1) 定义事件总线组件
在创建Vue实例时,在生命周期函数beforeCreate里面,为Vue原型对象加上一个属性,属性名为$bus,值为当前的Vue实例
new Vue({
...
beforeCreate() {
Vue.prototype.$bus = this // 安装全局事件总线,$bus 就是当前应用的 vm
},
...
})
2)在接收数据的组件里面为$bus绑定事件并定义回调函数
3)在发送数据的组件中触发事件
10 事件总线示例
1)main.js
在Vue原型对象上加上$bus
import Vue from 'vue'
import App from './App.vue' Vue.config.productionTip = false new Vue({
render: h => h(App),
beforeCreate() {
Vue.prototype.$bus = this // 安装全局事件总线,$bus 就是当前应用的 vm
},
}).$mount('#app')
2) SchoolComp.vue
接收数据的组件,为$bus绑定事件student,并定义回调函数
<template>
<div>
<h1 >{{schoolname}}</h1>
<StudentComp ></StudentComp> </div>
</template> <script>
import StudentComp from './StudentComp' export default {
name:'SchoolComp',
data(){
return {
schoolname:'实验小学1',
age:18
}
},
components:{
StudentComp
},
methods: { },
mounted() { this.$bus.$on('student', (stname)=>{
console.log(this) //Student的vc对象
console.log(stname) //Student的vc对象 }) }, }
</script> <style> </style>
3)StudentComp.vue
发送数据的组件,触发事件
<template>
<div>
<h1>{{stname}}</h1>
<button v-on:click="showName">点击</button>
</div> </template> <script> export default {
name:'StudentComp',
data(){
return {
stname:'小新'
}
},
methods: {
showName(){
console.log('在StudentComp中触发student事件')
this.$bus.$emit('student',this.stname)
}
},
}
</script>
4)效果

11 事件总线注意事项
由于所有的事件都是绑定在$bus上面的,当事件多的时候,注意事件名称的管理
Vue29 自定义事件及消息总线的更多相关文章
- vue_组件间通信:自定义事件、消息发布与订阅、槽
自定义事件 只能用于 子组件 向 父组件 发送数据 可以取代函数类型的 props 在父组件: 给子组件@add-todo-event="addTodo" 在子组件: 相关方法中, ...
- Android组件化方案及组件消息总线modular-event实战
背景 组件化作为Android客户端技术的一个重要分支,近年来一直是业界积极探索和实践的方向.美团内部各个Android开发团队也在尝试和实践不同的组件化方案,并且在组件化通信框架上也有很多高质量的产 ...
- C#中如何截取Windows消息来触发自定义事件
原文 C#中如何截取Windows消息来触发自定义事件 在c#windows开发中,我们常常会遇到拦截windows消息,来触发某个特定任务的问题. 由于目前使用c#的开发人员非常多,而且大多数c#程 ...
- 使用SignalR为FineUI/Webform打造消息总线
第一次写博客,语言组织能力不好,请大家多多包涵! 效果图如下: 图片的右下角即为SignalR消息总线的消息框. 一.建立SignalR服务端 第一步:打开一个空的FineUI 4.5空项目文件,在空 ...
- 使用SignalR打造消息总线
使用SignalR为FineUI/Webform打造消息总线 第一次写博客,语言组织能力不好,请大家多多包涵! 效果图如下: 图片的右下角即为SignalR消息总线的消息框. 一.建立SignalR服 ...
- springcloud(九):配置中心和消息总线(配置中心终结版)
我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...
- SpringCloud基于消息总线的配置中心
@https://www.cnblogs.com/ityouknow/p/6931958.html Spring Cloud Bus Spring cloud bus通过轻量消息代理连接各个分布的节点 ...
- SpringCloud消息总线
我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...
- [转]springcloud(九):配置中心和消息总线(配置中心终结版)
https://www.cnblogs.com/ityouknow/p/6931958.html springcloud(九):配置中心和消息总线(配置中心终结版) 我们在springcloud(七) ...
- Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus
背景 对于Android系统来说,消息传递是最基本的组件,每一个App内的不同页面,不同组件都在进行消息传递.消息传递既可以用于Android四大组件之间的通信,也可用于异步线程和主线程之间的通信.对 ...
随机推荐
- hutool包的DateUtil工具类
[首先引入依赖 ] <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-core& ...
- 【笔记】入门DP
复习一下近期练习的入门 \(DP\) .巨佬勿喷.\(qwq\) 重新写一遍练手,加深理解. 代码已经处理,虽然很明显,但请勿未理解就贺 \(qwq\) 0X00 P1057 [NOIP2008 普及 ...
- 给ofo共享单车撸一个微信小程序
想学一下微信小程序,发现文档这东西,干看真没啥意思.所以打算自己先动手撸一个.摩拜单车有自己的小程序,基本功能都有,方便又小巧,甚是喜爱.于是我就萌生了一个给ofo共享单车撸一个小程序(不知道为啥of ...
- CopyOnWriteArrayList 是如何保证线程安全的?
本文已收录到 AndroidFamily,技术和职场问题,请关注公众号 [彭旭锐] 提问. 前言 大家好,我是小彭. 在上一篇文章里,我们聊到了ArrayList 的线程安全问题,其中提到了 Copy ...
- DHorse操作手册
在介绍DHorse的操作之前,我们先来看一下发布一个系统的流程是什么样的. 发布系统的流程 我们以一个Springboot系统为例,来说明一下发布流程. 1.首先从代码仓库下载代码,比如Gitlab: ...
- SQL语句查询关键字 多表查询
目录 SQL语句查询关键字 select from 编写顺序和查询数据 前期数据准备 编写SQL语句的小技巧 查询关键字之筛选 where 逻辑运算符 not and or between not b ...
- 教你铁威马NAS中如何进行阵列升级
磁盘阵列 (RAID) 是磁盘阵列的管理工具.当TNAS 中安装的硬盘多于1个时,组建适当的磁盘阵列能提高硬盘的存储效率,提高数据的安全性. 磁盘阵列升级,比如,将原来是RAID 0 或者RAID 1 ...
- [机器学习] PCA主成分分析原理分析和Matlab实现方法
转载于http://blog.csdn.net/guyuealian/article/details/68487833 网上关于PCA(主成分分析)原理和分析的博客很多,本博客并不打算长篇大论推论PC ...
- VMware安装linux系统CentOs7.4 mini版过程
创建虚拟机 新建虚拟机 选择虚拟机的操作系统,本文中安装的CentOS属于linux 设置虚拟机的名称和虚拟机所使用的文件再物理机中的路径, 设置虚拟机的cup数量和核心数量,如果设置的不合适可以再创 ...
- YMOI 2019.6.29
题解 YMOI 2019.6.29 放弃FAIOJ,用cena考了一次试.被全方位吊打.. T1 开灯 题面: 在一条无限长的路上,有一排无限长的路灯,编号为1,2,3,4,--. 每一盏灯只有两种可 ...