1.安装mitt:npm install mitt -save

2. 新建EventBus.js文件:

// 事件总线第三方库:import mitt from 'mitt';const bus = mitt();export default bus;

3.发出事件的页面:bb.vue

小b页面视图

import bus from "../utils/EventBus"// import {ref} from "vue"const sendData = () => {bus.emit("data",18)}.box{text-align: left;}</style

4.接收事件的页面:dd.vue

小d页面视图

import bus from "../utils/EventBus"import { ref ,onMounted} from "vue";onMounted(()=>{bus.on("data",(info)=>{console.log("dd页面接收到的值:",info)})})

5.点击bb页面按钮: