VUE使用BUS进行兄弟组件传值

1.新建bus.js
import Vue from 'vue'
export default  new Vue
2.在需要传值和接受值的vue文件中,各自引入bus.js
import bus from '../../../utils/bus' // 在src目录的下面
3.定义传值的方法,使用bus.$emit(‘methodName’,data)
4.要接收值的组件里,使用bus.on(‘methodName’,val =>{ })
5 具体示例如下所示

index.vue

<template>
  <div>
    <Child1></Child1>
    <Child2></Child2>
  </div>
</template>

<script>
import Child1 from './module/child1'
import Child2 from './module/child2'
export default {
  components: {
    Child1,
    Child2
  }
}
</script>

child1.vue

<template>
  <div>
    <button @click="trans()">传值</button>
  </div>
</template>
<script>
import bus from '../../../utils/bus'
export default {
  name: "Child1",
  data() {
    return {
      helloData: "hello"
    };
  },
  methods: {
    trans() {
      bus.$emit('test', this.helloData) // 传值
    }
  },
}
</script>

child2.vue

<template>
  <div>
    {{cdata}}
  </div>
</template>
<script>
import bus from '../../../utils/bus'

export default {
  name: "Child2",
  data() {
    return {
      cdata: "子数据"
    };
  },
  mounted() {
    bus.$on('test', val => { // 接收值
      console.log(val);
      this.cdata = val
    })
  }
}
</script>

文章借鉴自:https://www.cnblogs.com/luguankun/p/11701121.html

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
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>

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值