vue 3.0 中使用 子组件点击触发父组件中的函数逻辑(emit)

父组件代码部分

<template>
  <div class="home">
  <HelloWorld ref="hello" :msg="count" @helloEmit="sayHello" />
  </div>
</template>
<script>

export default {
      
      setup (props, { attrs, slots }) {
      
      function sayHello (params) {
       alert(params)
      }

       return {sayHello}

      }

}

</script>

子组件代码

<template>
  <div>
    <div @click="helloEmit">你好</div>
  </div>
</template>

<script>
export default {

setup (props, context) {

      function helloEmit () {
      context.emit("helloEmit", "hello")
     }

      return {
       helloEmit
    }
}

};
</script>

``
原文地址:https://www.cnblogs.com/shiazhen/p/13489843.html