vue3+ts 调用父组件方法/ vue3+ts 使用emit

子组件代码
<script setup lang="ts">
import { defineEmits } from 'vue'
 
const emit = defineEmits(['callParentMethod'])
 
function triggerParentMethod() {
  emit('callParentMethod')
}
</script>
 
<template>
  <button @click="triggerParentMethod">Call Parent Method</button>
</template>

父组件代码

<template>
  <ChildComponent @callParentMethod="parentMethod" />
</template>
 
<script setup lang="ts">
import ChildComponent from './ChildComponent.vue'
 
function parentMethod() {
  console.log('Parent method called')
}
</script>

 

posted @ 2024-12-11 17:31  龙卷风吹毁停车场  阅读(185)  评论(0)    收藏  举报