Vue3中跨组件传值(Mitt)

1.安装mitt

我使用的是yarn

yarn add mitt

2.在根目录下创建eventBus.js

// eventBus.js
import mitt from "mitt";
 
export const EventBus = mitt();

3.组件间进行传值与接收值

使用emit传值,使用on接收,最后不要忘记销毁,避免内存泄漏

<template>
  <div class="parent">
    <h3>A组件</h3>
    <button @click="handleClick">传递Content值</button>
  </div>
</template>
 
<script setup>
import { ref } from "vue";
import { EventBus } from "@/eventBus.js";
 
const content = ref("Hello Word");
 
const handleClick = () => {
  EventBus.emit("change-content", content);
};
</script>
 
<style lang="scss" scoped>
.parent {
  width: 200px;
  height: 200px;
  background-color: #ccc;
}
</style>
<template>
  <div class="nav">
    <h2>B组件</h2>
    <p>接受的数据:{{ contents }}</p>
  </div>
</template>
 
<script setup>
import { EventBus } from "@/eventBus.js";
import { onMounted, onUnmounted, ref } from "vue";
const contents = ref("");
 
const handleMessage = (data) => {
  console.log(data);
  contents.value = data.value;
};
 
onMounted(() => {
  EventBus.on("change-content", handleMessage);
});
 
onUnmounted(() => {
  EventBus.off("change-content");
});
</script>
 
<style lang="scss" scoped>
.nav {
  width: 200px;
  height: 200px;
  background-color: #ccc;
}
</style>

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、付费专栏及课程。

余额充值