// 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');
网友评论