자식은 이벤트를 발생시켜 부모에게 위로 통신합니다. 부모는 @event-name으로 이를 수신합니다. 이것이 Vue의 단방향 데이터 흐름을 완성합니다: props는 아래로, events는 위로.
자식에서 발생시키기
vue
<!-- TodoItem.vue -->
<script setup>
const props = defineProps({ todo: Object });
const emit = defineEmits(["delete", "toggle"]); // 발생시킬 이벤트를 선언
function onDelete() {
emit("delete", props.todo.id); // 페이로드와 함께 이벤트 발생
}
</script>
<template>
<li>
{{ todo.text }}
<button @click="onDelete">Delete</button>
<button @click="emit('toggle', todo.id)">Toggle</button>
</li>
</template>
