vue3 - 19.Mitt兄弟组件传参传值

1.安装mitt:

npm install mitt -S

2.使用方法:

main.ts

import { createApp } from 'vue'
import App from './App.vue'
import mitt from 'mitt'
 
const Mit = mitt()
 
//TypeScript注册
// 由于必须要拓展ComponentCustomProperties类型才能获得类型提示
declare module "vue" {
    export interface ComponentCustomProperties {
        $Bus: typeof Mit
    }
}
 
const app = createApp(App)
 
//Vue3挂载全局API
app.config.globalProperties.$Bus = Mit
 
app.mount('#app')

子组件组件:A.vue == > 派发 $emit

<template>
    <div>
        我是A组件
        <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-click','hello world')
    }
</script>
<style scoped>
</style>

子组件组件:B.vue == > 监听 $on

<template>
    <div>
        我是B组件 ==> {{ getStr }}
    </div>
</template>
<script setup lang='ts'>
    import { ref } from 'vue'
    import { getCurrentInstance } from 'vue'
    const getStr = ref<unknown>('');
    const instance = getCurrentInstance()
    instance?.proxy?.$Bus.on('on-click', (str) => {
        console.log(str)
        getStr.value = str
    })
</script>
<style scoped>
</style>

父页面:

<template>
  <div>
    <A />
    <B />
  </div>
</template>
<script setup lang='ts'>
  import { ref } from 'vue'
  import A from './A.vue'
  import B from './B.vue'
</script>
<style scoped>
</style>

3.通过mitt传多条数据:

子组件组件:A.vue == > 派发多个

<template>
    <div>
        我是A组件
        <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-click1','hello')
        instance?.proxy?.$Bus.emit('on-click2','world')
    }
</script>
<style scoped>
</style>

子组件组件:B.vue == > 监听多个 $on('*')

<template>
    <div>
        我是B组件 ==> {{ getStr1 }} - {{getStr2}}
    </div>
</template>
<script setup lang='ts'>
    import { ref } from 'vue'
    import { getCurrentInstance } from 'vue'
    const getStr1 = ref<unknown>('');
    const getStr2 = ref<unknown>('');
    const instance = getCurrentInstance()
    instance?.proxy?.$Bus.on('*', (type,str) => {
        console.log(type,str)
        if(type == 'on-click1'){
            getStr1.value = str
        }
        if(type == 'on-click2'){
            getStr2.value = str
        }
    })
</script>
<style scoped>
</style>

父页面:

<template>
  <div>
    <A />
    <B />
  </div>
</template>
<script setup lang='ts'>
  import { ref } from 'vue'
  import A from './A.vue'
  import B from './B.vue'
</script>
<style scoped>
</style>

移除监听事件(off)

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值