父组件:
<script setup>
import Son from './Son.vue'
const getMsg=(msg)=>{
console.log(msg)
}
</script>
<template>
<div>
<h2>父组件----子传父</h2>
<!-- 1. 绑定事件-->
<Son @getMsg="getMsg"></Son>
</div>
</template>
子组件:
<script setup>
// 2. 通过defineEmits
const emit = defineEmits(['getMsg'])
const sendMsg=()=>{
emit('getMsg','sonMessage')
}
</script>
<template>
<div>
<h2>子组件----子传父</h2>
<button @click="sendMsg">触发自定义事件</button>
</div>
</template>