子组件

child.vue

<template>
  <div>
    <p>hello world</p>
    <button @click="handleClick">click</button>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue";
export default defineComponent({
  emits: ["click"],
  setup(prop, context) {
    const handleClick = () => {
      context.emit("click");
    };
    return {
      handleClick
    };
  }
});
</script>

子组件中使用context.emit传递出去。

值得注意的是:emits

emits: ["click"]

将自定义的名称需要再emits中声明

 

父组件

father.vue

<template>
  <el-button type="primary" @click="setClick"></el-button>
</template>

<script lang="ts">
export default {
  setup() {
    const setClick = () => {
      console.log("click");
    };
    return { setClick };
  }
};
</script>

父组件还是一样

相关文章:

  • 2021-08-17
  • 2022-12-23
  • 2021-08-31
  • 2021-12-30
  • 2022-12-23
  • 2022-02-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-12-26
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-06-13
  • 2023-01-16
  • 2022-12-23
相关资源
相似解决方案