Data transfer of sibling components in Vue3

Install and use mitt

There is no need to write any complicated code, and the usage method is almost the same as that of vue2

$ npm install --save mitt

mount

//main.js中
import mitt from "mitt"
import { createApp } from "vue"
 
const app = createApp({})//正常配置
//挂载事务总线
app.config.globalProperties.$bus = new mitt()

use

//在组件A中使用事务总线触发某个动作
this.$bus.emit("EVENTTYPE");
 
//在组件B中监听动作的发生
this.$bus.on("EVENTTYPE",()=>{
    console.log("EVENTTYPE发生了")})

Guess you like

Origin blog.csdn.net/qq_27318177/article/details/119145002