方法1

先定义 emit 再调用

  1. <script setup>
  2. const emit = defineEmits(['eventA', 'eventB'])
  3. function btnClick(params) {
  4. emit('eventA')
  5. emit('eventB', params)
  6. }
  7. </script>

方法2

使用 useContext

  1. <template>
  2. <button @click="action('🎉')</button>
  3. <!-- emit is defined through useContext -->
  4. <button @click="emit('action1','🥑')">1</button>
  5. <!-- $emit here doesn't need to be defined -->
  6. <button @click="$emit('action2','🦄')">2</button>
  7. </template>
  8. <script setup>
  9. import { useContext } from 'vue'
  10. const { emit } = useContext()
  11. const action = (id) => emit('action', id);
  12. </script>

方法3

使用

  1. <script setup>
  2. import { getCurrentInstance} from 'vue'
  3. const {emit} = getCurrentInstance()
  4. </script>

参考