父组件代码:
<template> <div> <child-component @update:message="handleMessage"></child-component> </div> </template> <script setup> import ChildComponent from './ChildComponent.vue'; function handleMessage(value) { console.log('接收到子组件传递的值:', value); } </script>
子组件代码:
<template> <div> <button @click="sendMessage">发送消息给父组件</button> </div> </template> <script setup> import { ref, defineEmits } from 'vue'; const message = ref(''); const emits = defineEmits(['update:message']); function sendMessage() { emits('update:message', message.value); } </script>