vue3+ts mitt的使用

javascript 复制代码
安装mitt :npm  i  mitt -S

main.ts:

javascript 复制代码
import  mitt  from  'mitt'
 const  Mit = mitt();
 declare module 'vue' {
  export interface ComponentCustomProperties{
    $Bus:typeof  Mit
  }
}
app.config.globalProperties.$Bus=Mit

在A组件中使用

handlebars 复制代码
<template>
    <div>
     <h1>我是A</h1>
     <button @click="emit">emit</button>
    </div>
</template>
  
<script setup lang='ts'>
  import  {getCurrentInstance } from  "vue";
  const  instance=getCurrentInstance();
  const  emit=  ()=>{
         instance?.proxy?.$Bus.emit("on-xiaoman","你是个小可爱")
  }
</script>
  
<style>
  
</style>

在B组件中使用:

handlebars 复制代码
<template>
    <div>
     <h1>我是B</h1>
    
    </div>
</template>
  
<script setup lang='ts'>
  import  {getCurrentInstance } from  "vue";
  const  instance=getCurrentInstance();
  instance?.proxy?.$Bus.on('on-xiaoman',(str)=>{
console.log(str,'+++++++++B')
  })
</script>
  
<style>
  
</style>

效果:

相关推荐
慢半拍iii2 分钟前
数据结构——D/串
c语言·开发语言·数据结构·c++
爱学习的白杨树36 分钟前
Sentinel介绍
java·开发语言
Frankabcdefgh42 分钟前
Python基础数据类型与运算符全面解析
开发语言·数据结构·python·面试
kaiaaaa1 小时前
算法训练第十五天
开发语言·python·算法
风吹头皮凉2 小时前
vue实现气泡词云图
前端·javascript·vue.js
南玖i2 小时前
vue3 + ant 实现 tree默认展开,筛选对应数据打开,简单~直接cv
开发语言·前端·javascript
南枝异客2 小时前
三数之和-力扣
开发语言·javascript·数据结构·算法·leetcode·排序算法
小钻风33662 小时前
深入浅出掌握 Axios(持续更新)
前端·javascript·axios
爱意随风起风止意难平2 小时前
AIGC 基础篇 Python基础 05 元组,集合与字典
开发语言·python·aigc
鸢时望巧2 小时前
Shell循环(二)
运维·开发语言