前端学习-vue学习011-Emit

官方教程链接

子组件还可以向父组件触发事件

  • 子组件声明触发的事件,并且带参数触发
<script lang="ts" setup>
   const emit = defineEmits(['res'])
   emit('res','hi thisismy msg') // 第一个参数是事件名称,其他参数都会传给父组件
</script>
  • 父组件接收参数
<template>
  <Todo/>
  <Props @res="(msg) => { childMsg = msg}"/>
  <p>{{ childMsg }}</p>
</template>

<script lang="ts" setup>
  import Emit from './components/Emit.vue'
  const childMsg = ref('no msg yet')
  
</script>
posted @ 2024-03-21 16:13  ayubene  阅读(28)  评论(0)    收藏  举报