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 自定义事件及消息总线的更多相关文章

  1. vue_组件间通信:自定义事件、消息发布与订阅、槽

    自定义事件 只能用于 子组件 向 父组件 发送数据 可以取代函数类型的 props 在父组件: 给子组件@add-todo-event="addTodo" 在子组件: 相关方法中, ...

  2. Android组件化方案及组件消息总线modular-event实战

    背景 组件化作为Android客户端技术的一个重要分支,近年来一直是业界积极探索和实践的方向.美团内部各个Android开发团队也在尝试和实践不同的组件化方案,并且在组件化通信框架上也有很多高质量的产 ...

  3. C#中如何截取Windows消息来触发自定义事件

    原文 C#中如何截取Windows消息来触发自定义事件 在c#windows开发中,我们常常会遇到拦截windows消息,来触发某个特定任务的问题. 由于目前使用c#的开发人员非常多,而且大多数c#程 ...

  4. 使用SignalR为FineUI/Webform打造消息总线

    第一次写博客,语言组织能力不好,请大家多多包涵! 效果图如下: 图片的右下角即为SignalR消息总线的消息框. 一.建立SignalR服务端 第一步:打开一个空的FineUI 4.5空项目文件,在空 ...

  5. 使用SignalR打造消息总线

    使用SignalR为FineUI/Webform打造消息总线 第一次写博客,语言组织能力不好,请大家多多包涵! 效果图如下: 图片的右下角即为SignalR消息总线的消息框. 一.建立SignalR服 ...

  6. springcloud(九):配置中心和消息总线(配置中心终结版)

    我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...

  7. SpringCloud基于消息总线的配置中心

    @https://www.cnblogs.com/ityouknow/p/6931958.html Spring Cloud Bus Spring cloud bus通过轻量消息代理连接各个分布的节点 ...

  8. SpringCloud消息总线

    我们在springcloud(七):配置中心svn示例和refresh中讲到,如果需要客户端获取到最新的配置信息需要执行refresh,我们可以利用webhook的机制每次提交代码发送请求来刷新客户端 ...

  9. [转]springcloud(九):配置中心和消息总线(配置中心终结版)

    https://www.cnblogs.com/ityouknow/p/6931958.html springcloud(九):配置中心和消息总线(配置中心终结版) 我们在springcloud(七) ...

  10. Android消息总线的演进之路:用LiveDataBus替代RxBus、EventBus

    背景 对于Android系统来说,消息传递是最基本的组件,每一个App内的不同页面,不同组件都在进行消息传递.消息传递既可以用于Android四大组件之间的通信,也可用于异步线程和主线程之间的通信.对 ...

随机推荐

  1. MySQL InnooDB引擎之并发事务问题以及隔离级别的作用和区别

    最近在复习MySQL事务,但网上很多博客和资料可以说讲的不是模棱两可就是只有文字描述不够形象易懂,下面通过我的学习来详细讲一讲事务并发都会引起哪些问题?以及隔离级别是什么?InnoDB引擎是如何通过隔 ...

  2. Bugku login1

    打开是个普普通通的登录界面,盲猜是注入题,先看看源码吧,没找到什么有用的信息,那就先注册试试 注册admin就已经存在,可能待会就爆破admin的密码也可能,因为没有验证嘛 试试注册其他的 登录发现他 ...

  3. 解决"VLC 无法打开 MRL「screen://」。详情请检查日志" 问题

    问题描述 vlc 抓取桌面视频报这个错误 解决 sudo apt-get install vlc-plugin-access-extra 其他 不一定每次都在图形化界面调用,也可以直接在终端输入 vl ...

  4. kettel

    下载教程:(目前最高版本7.1) 1.网址:https://community.hitachivantara.com/docs/DOC-1009855 2.

  5. 100以内能被7整除的前五个数-Java

    import java.util.HashSet; import java.util.Set; public class Demo { //100以内能够被7整除的前五个数 public static ...

  6. 关于盒子动态高度与transition的问题

    今天遇到个小问题 大概要实现类似手风琴的效果 本来设计是定死的高度,直接 height:0; - > height:xxxpx;但之后要改成动态变化的高度,手风琴展开后是个列表,并且列表每行高度 ...

  7. Spring中11个最常用的扩展点,你知道几个?

    前言 在使用spring的过程中,我们有没有发现它的扩展能力很强呢? 由于这个优势的存在,使得spring具有很强的包容性,所以很多第三方应用或者框架可以很容易的投入到spring的怀抱中.今天我们主 ...

  8. python 之列表(list)处理

    列表(list) 创建一个列表,只要把逗号分隔的不同的数据项使用方括号括起来即可,一个列表中的数据类型可以各不相同,可以同时分别为整数.实数.字符串等基本类型,甚至是列表.元组.字典.集合以及其他自定 ...

  9. uniapp 打包app 引入高德地图

    一.高德地图注册key值 二.项目中添加配置 三.项目中引用 <view class="home-btom-box" > <view class="ho ...

  10. vue中mixins(混入)的用法

    vue中mixin的使用详解 混入 (mixin) 提供了一种非常灵活的方式,来分发 Vue 组件中的可复用功能.一个混入对象可以包含任意组件选项.当组件使用混入对象时,所有混入对象的选项将被&quo ...