vue中使用bus来实现不同组件的传值(更推荐vuex)

前言:

      在vue中实现用公共bus来实现不同组件直接的传值。

实现方法:

1、main.js中在window上挂载一个变量EventBus 

window.EventBus = new Vue()

2、传方法页面,必须在页面的销毁阶段传方法,至于原因,请看最下面的原因


beforeDestroy () {
    window.EventBus.$emit('getData', 123)
  }

3、接受方法页面,必须在created ,至于原因,请看最下面的原因

//注意:这里只能在beforeCreate ,created,和beforeMount这三个钩子函数中监听$on。
created () {
   
    window.EventBus.$on('getData', (msg) => this.getData(msg))
  }
 destroyed () {
    window.EventBus.$off('getData')
  }

为什么传要放a页面的befireDestroy里面,获取b要放create,并要加off关闭原因:

参考地址:https://www.cnblogs.com/ljh-dream/articles/10048265.html

 

### Vue2 组件方法 在 Vue2 中,组件间的可以通过多种方式进行实现。以下是几种常见的方式及其具体实现。 #### 1. **父子组件之间的** - 子组件向父组件递数据通常通过 `this.$emit` 实现[^2]。 - 父组件向子组件递数据则通过 `props` 属性完成。 ```vue <!-- ParentComponent.vue --> <template> <div> <ChildComponent :message="parentMessage" @childEvent="handleChildEvent"></ChildComponent> </div> </template> <script> import ChildComponent from './ChildComponent.vue'; export default { components: { ChildComponent }, data() { return { parentMessage: 'Hello from Parent' }; }, methods: { handleChildEvent(dataFromChild) { console.log('Data received from child:', dataFromChild); } } }; </script> ``` ```vue <!-- ChildComponent.vue --> <template> <button @click="sendToParent">Send to Parent</button> </template> <script> export default { props: ['message'], methods: { sendToParent() { this.$emit('childEvent', 'Hello from Child'); } } }; </script> ``` --- #### 2. **兄弟组件之间的** 兄弟组件之间无法直接通信,因此需要借助父组件作为中介来实现数据的递。 ##### 流程描述: - 首先,第一个子组件通过 `$emit` 将事件触发并携带数据发送到父组件。 - 接着,父组件监听该事件并将接收到的数据重新分配给第二个子组件。 ```vue <!-- ParentComponent.vue --> <template> <div> <BrotherA @data-from-a="updateSiblingB"></BrotherA> <BrotherB :siblingaData="siblingAData"></BrotherB> </div> </template> <script> import BrotherA from './BrotherA.vue'; import BrotherB from './BrotherB.vue'; export default { components: { BrotherA, BrotherB }, data() { return { siblingAData: null }; }, methods: { updateSiblingB(data) { this.siblingAData = data; } } }; </script> ``` ```vue <!-- BrotherA.vue --> <template> <button @click="sendData">Send Data to Sibling B</button> </template> <script> export default { methods: { sendData() { const message = 'Hello from A'; this.$emit('data-from-a', message); } } }; </script> ``` ```vue <!-- BrotherB.vue --> <template> <p>Data Received from A: {{ siblingaData }}</p> </template> <script> export default { props: ['siblingaData'] }; </script> ``` --- #### 3. **跨级组件之间的 (祖孙关系)** 对于复杂的场景,比如祖父组件与孙子组件之间的通信,可以采用以下两种方式: - 使用 `$refs` 或者 `$children` 来手动访问嵌套层次中的实例对象。 - 推荐的是引入全局状态管理工具 Vuex,或者使用事件总线 EventBus 进行解耦处理。 ##### 示例:基于 Event Bus 的跨级组件通信 创建一个独立的事件中心文件用于订阅和发布消息。 ```javascript // eventBus.js import Vue from 'vue'; export const EventBus = new Vue(); ``` ```vue <!-- GrandfatherComponent.vue --> <template> <GrandsonComponent></GrandsonComponent> </template> <script> import { EventBus } from './eventBus.js'; export default { mounted() { setTimeout(() => { EventBus.$emit('grandpa-message', 'Hello from Grandpa!'); }, 1000); } }; </script> ``` ```vue <!-- GrandsonComponent.vue --> <template> <p>Message from grandpa: {{ grandpaMessage }}</p> </template> <script> import { EventBus } from './eventBus.js'; export default { data() { return { grandpaMessage: '' }; }, created() { EventBus.$on('grandpa-message', (msg) => { this.grandpaMessage = msg; }); } }; </script> ``` --- ### 总结 以上介绍了 Vue2 中不同类型的组件的具体方法以及其实现细节。无论是简单的父子组件还是复杂的关系链路,都可以找到合适的解决方案以满足实际开发需求。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浩星

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值