• Vue 全局事件总线


    一种可以在任意组件间通信的方式,本质上就是一个对象,它必须满足以下条件

    1.所有的组件对象都必须能看见他

    2.这个对象必须能够使用$on $emit $off 方法去绑定、触发和解绑事件

    在这里插入图片描述
    在这里插入图片描述
    使用步骤:

    1.定义全局事件总线

    new Vue({
       	...
       	beforeCreate() {
       		Vue.prototype.$bus = this // 安装全局事件总线,$bus 就是当前应用的 vm
       	},
        ...
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.使用事件总线

    a.接收数据: A组件想接收数据,则在A组件中给 $bus 绑定自定义事件,事件的回调函数留在A组件自身

    export default {
        methods(){
            demo(data){...}
        }
        ...
        mounted() {
            this.$bus.$on('xxx',this.demo)
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    b.提供数据 this. b u s . bus. bus.emit(‘xxx’,data)

    3.最好再 beforeDestory 钩子中,用 $off() 去解绑当前组件所用到的事件

    src/main.js

    import Vue from 'vue'
    import App from './App.vue'
    
    Vue.config.productionTip = false
    
    new Vue({
      el:'#app',
      render: h => h(App),
      beforeCreate() {
        Vue.prototype.$bus = this // 安装全局事件总线
      }
    })
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    src/App.vue

    <template>
    	<div class="app">
    		<School/>
    		<Student/>
    	</div>
    </template>
    
    <script>
    	import Student from './components/Student'
    	import School from './components/School'
    
    	export default {
    		name:'App',
    		components:{ School, Student }
    	}
    </script>
    
    <style scoped>.app{background-color: gray;padding: 5px;}</style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    src/components/School.vue

    <template>
      <div class="school">
        <h2>学校名称:{{ name }}</h2>
        <h2>学校地址:{{ address }}</h2>
      </div>
    </template>
    
    <script>
      export default {
        name: "School",
        data() {
          return {
            name: "xxxxxxxx",
            address: "北京",
          };
        },
        mounted() {  //🔴
          // console.log('School',this)
          this.$bus.$on("hello", (data) => {
            console.log("我是School组件,收到了数据", data);
          });
        },
        beforeDestroy() {  //🔴
          this.$bus.$off("hello");
        },
      };
    </script>
    
    <style scoped>.school {background-color: skyblue;padding: 5px;}</style>
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    src/components/Student.vue

    <template>
      <div class="student">
        <h2>学生姓名:{{ name }}</h2>
        <h2>学生性别:{{ sex }}</h2>
        <button @click="sendStudentName">把学生名给School组件</button> //🔴
      </div>
    </template>
    
    <script>
      export default {
        name:'Student',
        data() {
          return {
            name:'张三',
            sex:'男'
          }
        },
        methods: {  //🔴
          sendStudentName(){
            this.$bus.$emit('demo', this.name)
          }
        }
      }
    </script>
    
    <style scoped>.student{background-color: pink;padding: 5px;margin-top: 30px;}</style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
  • 相关阅读:
    使用halcon实现基于深度学习的目标检测
    【编译原理】LL(1)文法
    物联网平台通用后端架构设计
    AR工业远程巡查系统:实时监控设备状态,及时发现潜在问题
    【Docker 基础教程】容器数据持久化(二) ------ Mysql的基础配置
    数据分析与挖掘———SPSS Moderler
    Java新手小白入门篇 API - 多线程
    大数据ClickHouse(十五):万字长文最详ClickHouse SQL语法之DDL 操作讲解
    Scrapy案例(一)
    Matlab零基础入门
  • 原文地址:https://blog.csdn.net/fd2025/article/details/125430210