emits

263 阅读1分钟
<template>
  <button @click="$emit('click')">click me!</button>
</template>

<script lang="ts">
import { reactive, ref, watch } from 'vue';
export default {
  name: 'Count',
  /** 该事件现在会被触发两次:
    一次来自 $emit()。
    另一次来自应用在根元素上的原生事件监听器。
  */
  emits: [], // 未定义
  setup() {
    
  }
};
</script>
<template>
  <button @click="$emit('my-click')">click me!</button>
</template>

<script lang="ts">
import { reactive, ref, watch } from 'vue';
export default {
  name: 'Count',
  // 和 prop 类似,现在可以通过 emits 选项来定义组件可触发的事件:
  // 该选项也可以接收一个对象,该对象允许开发者定义传入事件参数的验证器,和 props 定义里的验证器类似。
  emits: ['my-click'],
  setup() {}
};
</script>