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>

``
posted @ 2020-08-12 12:03  阿臻  阅读(4459)  评论(0编辑  收藏  举报