Vue2中兄弟组件的传值

1.为什么要有事件总线

在vue中非常常见的有父子组件通信和兄弟组件通信,而兄弟组件的通信就需要用到了事件总线(EventBus),将总线作为一个中间的桥梁进行通信

2.EventBus的使用步骤

因为在Vue实例的原型链上有着$emit,$on这些方法,所有我们可以借助Vue的实例作为通信的中间桥梁

a. 创建一个Vue的实例对象(new Vue()),并绑定到Vue的原型链上,这样在任意组件中都可以调用

b.在数据发送方,调用$emit('事件名称',要发送的数据)方法触发自定义事件

c.在数据接收方,调用$on("事件名称",事件处理函数)方法注册一个自定义事件

// 在Vue原型链上绑定一个$bus的事件总线

Vue.prototype.$bus = new Vue();

数据发送方

// 以在Vue组件中为例

export default{
    data(){
        return{
            msg: "hello,vue.js"
        }
    },
    methods: {
        // 在方法中发送数据
        sendMsg(){
            this.$bus.$emit("share",this.msg);
        }
    }   
}

数据接收方:

export default{
    data(){
        return {
            msgFrom: ""
        }
    },
    created(){
        // 回调函数也可以抽出来写成一个方法在methods 里面
        this.$bus.$on("share",val => {
           this.msgFrom = val
        })
    },
}

移除绑定的事件

export default{
    data(){
        return {
            msgFrom: ""
        }
    },
    created(){
        // 这里把回调函数放在methods里面
        this.$bus.$on("share",this.myShare)
    },
    methods: {
        myShare(val){
           this.msgFrom = val;
        }
    },
    destroyed(){
        this.$bus.$off("share"); // 移除指定这个事件
        this.$bus.$off();  // 移除所有事件
    }
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3兄弟组件传值有多种方法,其两种常用的方法如下: 1.使用事件总线(Event Bus)进行传值,具体步骤如下: 在父组件创建一个事件总线实例,并在需要传值的地方触发事件,将数据作为参数传递给事件;在子组件监听该事件,并在回调函数获取传递的数据。示例代码如下: // 父组件 <template> <div> <h1>我是父组件</h1> <button @click="sendMsg">发送消息给子组件</button> </div> </template> <script> import { ref } from 'vue' import mitt from 'mitt' export default { setup() { const emitter = mitt() const msg = ref('父组件的消息') const sendMsg = () => { emitter.emit('getMsg', msg.value) } return { emitter, sendMsg } } } </script> // 子组件 <template> <div> <h1>我是子组件</h1> <p>{{ msg }}</p> </div> </template> <script> import { ref, onMounted } from 'vue' import mitt from 'mitt' export default { setup() { const emitter = mitt() const msg = ref('') onMounted(() => { emitter.on('getMsg', (data) => { msg.value = data }) }) return { emitter, msg } } } </script> 2.使用插件mitt进行传值,具体步骤如下: 在父组件和子组件都引入mitt插件,并在父组件创建一个mitt实例,然后在需要传值的地方调用emit方法,将数据作为参数传递给子组件;在子组件通过props接收父组件传递的数据。示例代码如下: // 父组件 <template> <div> <h1>我是父组件</h1> <button @click="sendMsg">发送消息给子组件</button> </div> </template> <script> import { ref } from 'vue' import mitt from 'mitt' export default { setup() { const emitter = mitt() const msg = ref('父组件的消息') const sendMsg = () => { emitter.emit('getMsg', msg.value) } return { emitter, sendMsg } } } </script> // 子组件 <template> <div> <h1>我是子组件</h1> <p>{{ msg }}</p> </div> </template> <script> import { ref, onMounted, reactive } from 'vue' import mitt from 'mitt' export default { props: { msg: { type: String, default: '' } }, setup(props) { const emitter = mitt() onMounted(() => { emitter.on('getMsg', (data) => { props.msg = data }) }) return { emitter } } } </script>

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值