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>

效果:

相关推荐
C语言不精14 小时前
c语言-优雅的多级菜单设计与实现
c语言·开发语言·算法
geekmice14 小时前
thymeleaf处理参数传递问题
开发语言·lua
LNN202214 小时前
Qt 5.8.0 下实现触摸屏热插拔功能的探索与实践(2)
开发语言·qt
董世昌4114 小时前
箭头函数和普通函数有什么区别
开发语言·javascript·ecmascript
AI科技星14 小时前
张祥前统一场论:引力场与磁矢势的关联,反引力场生成及拉格朗日点解析(网友问题解答)
开发语言·数据结构·经验分享·线性代数·算法
β添砖java14 小时前
python第一阶段第八章文件操作
开发语言·python
-森屿安年-14 小时前
二叉平衡树的实现
开发语言·数据结构·c++
脑极体14 小时前
蓝河入海:Rust先行者vivo的开源之志
开发语言·后端·rust·开源
foxsen_xia14 小时前
go(基础01)——协程
开发语言·算法·golang
源代码•宸14 小时前
GoLang并发简单例子(goroutine + channel + WaitGroup)
开发语言·经验分享·后端·学习·golang