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四大组件之间的通信,也可用于异步线程和主线程之间的通信.对 ...
随机推荐
- Codeforces Round #833 (Div. 2) A-D.md
比赛链接 A 题解 知识点:数学. 注意到 \(n\) 为奇数时,不考虑连续性,一共有 \(\lceil \frac{n}{2} \rceil ^2\) 个格子,接下来证明一定能凑成方块. 从下往上从 ...
- C#使用最小二乘法对多个离散点进行圆拟合
/// <summary> /// 最小二乘法拟合圆,计算拟合圆半径和拟合圆圆心 /// </summary> /// <param name="points& ...
- 用 vue3 中的 reduce(累加器) 随机生成100个字母,放入数组中,统计每个字母出现的次数
一.首先不用 reduce() 来实现 代码如下: <template lang=""> <div> <h1>统计每个字母出现的次数,不使用r ...
- 某工控图片上传服务 CPU 爆高分析
一:背景 1.讲故事 今天给大家带来一个入门级的 CPU 爆高案例,前段时间有位朋友找到我,说他的程序间歇性的 CPU 爆高,不知道是啥情况,让我帮忙看下,既然找到我,那就用 WinDbg 看一下. ...
- 有趣的 Go HttpClient 超时机制
hello,大家好呀,我是既写 Java 又写 Go 的小楼,在写 Go 的过程中经常对比这两种语言的特性,踩了不少坑,也发现了不少有意思的地方,今天就来聊聊 Go 自带的 HttpClient 的超 ...
- 24、编写一个函数void replace(char *str1,char *str2,int i,int j),将字符串中str1中的第i个字符开始到j个字符结束的位置替换为str2.
/* 编写一个函数void replace(char *str1,char *str2,int i,int j),将字符串中str1中的第i个字符开始到j个字符结束的位置替换为str2. */ #in ...
- 【云原生 · Kubernetes】部署高可用kube-scheduler集群
个人名片: 因为云计算成为了监控工程师 个人博客:念舒_C.ying CSDN主页️:念舒_C.ying 部署高可用kube-scheduler集群 13.1 创建 kube-scheduler 证 ...
- Kubernetes_k8s持久化存储(亲测可用)
一.前言 新建具有两个节点的k8s集群,主节点(master节点/m节点)的ip地址是192.168.100.150,从节点(w1节点)的ip地址是192.168.100.151. 本文操作如何将po ...
- Vue 路由跳转显示空白页面的问题
在写一个登录界面跳转到首页时,路由如下 export default new VueRouter({ routes: [ { path: "/", name: "Logi ...
- MyBatis02:流程分析、注解、代理dao实现CRUD、参数深入、传统DAO、配置
今日内容 回顾 mybatis的自定义.分析和环境搭建 完善基于注解的mybatis mybatis的curd(基于代理dao的方式)※ mybatis的参数深入及结果集的深入 mybatis中基于传 ...