美文网首页
vue3.0 使用 mitt 事件总线

vue3.0 使用 mitt 事件总线

作者: 如果俞天阳会飞 | 来源:发表于2022-04-25 17:09 被阅读0次
    //  main.ts
    
    import mitt from 'mitt';
    import App from './App.vue';
    type Events = {
        foo: string;
        bar?: number;
    };
    const app = createApp(App);
     app.config.globalProperties.$bus = mitt<Events>();
    
    // child.vue
    import { ref, getCurrentInstance, ComponentInternalInstance } from 'vue';
    const medicineName = ref<string>('');
    
    const { appContext } = getCurrentInstance() as ComponentInternalInstance;
    appContext.config.globalProperties.$bus.on('medicineName', (name:string) => {
      medicineName.value = name;
    });
    
    
    // emit.vue
    import {
      ref, getCurrentInstance, ComponentInternalInstance, defineEmits,
    } from 'vue';
    
    const { appContext } = getCurrentInstance()as ComponentInternalInstance;
    appContext.config.globalProperties.$bus.emit('medicineName',  '111111');
    

    相关文章

      网友评论

          本文标题:vue3.0 使用 mitt 事件总线

          本文链接:https://www.haomeiwen.com/subject/fdtqyrtx.html