emit是用于child组件向parent组件通信的工具,因为vue3的script可以设置为setup,写法同vue2有较大区别。
一、script setup - 直接写
<script lang="ts" setup>const emit = defineEmits(['close']);function handleClose() {emit('close');}
</script>
二、script - 需要在setup函数中接收
<script lang="ts">import { defineComponent, onMounted } from 'vue'export default defineComponent({emits: ['init'],setup(props, { emit }) {onMounted(() => {emit('init')})})
</script>