Vue에서 부모 ↔ 자식 간 데이터 흐름은 다음 두 가지가 핵심입니다:
✅ 1. 부모 → 자식: props
를 통해 전달
🔹 부모 컴포넌트
<ChildComponent :message="parentMessage" />
export default {
data() {
return {
parentMessage: '안녕하세요!'
}
}
}
🔹 자식 컴포넌트
<template>
<p>{{ message }}</p>
</template>
<script>
export default {
props: ['message'] // 부모가 전달한 데이터 수신
}
</script>
✅ 2. 자식 → 부모: $emit()
으로 이벤트 전달
🔹 자식 컴포넌트
<template>
<button @click="$emit('openModal', i)">상세보기</button>
</template>
$emit('이벤트명', 전달할값)
형태로 부모에게 알림
🔹 부모 컴포넌트
<ChildComponent @openModal="isModal=true; selectedMovie=$event" />
- 전달한 값 i는 $event로 받을 수 있다.
💡 요약
방향 | 방법 | 키워드 |
부모 → 자식 | 속성 전달 | props |
자식 → 부모 | 이벤트 발생 | $emit + @이벤트명 |
Share article