Contents
see List작성일 2022. 05. 24.
Vue.js 부모, 자식 컴포넌트 메소드 호출
Vue.js에서 부모-자식 컴포넌트 간 메소드를 호출하는 방법입니다. Props, Events, Refs를 활용하여 컴포넌트 간 통신을 구현합니다.
언제 사용하나요?
- 부모에서 자식 폼 유효성 검사 호출
- 자식에서 부모의 데이터 갱신 요청
- 모달 열기/닫기 제어
- 외부에서 컴포넌트 초기화
부모에서 자식 메소드 호출 (ref)
<!-- 부모 컴포넌트 -->
<template>
<div>
<ChildComponent ref="childRef" />
<button @click="callChildMethod">자식 메소드 호출</button>
</div>
</template>
<script>
import ChildComponent from "./ChildComponent.vue";
export default {
components: { ChildComponent },
methods: {
callChildMethod() {
// ref를 통해 자식 메소드 호출
this.$refs.childRef.childMethod();
// 파라미터 전달
this.$refs.childRef.updateData({ name: "홍길동" });
}
}
};
</script>
<!-- 자식 컴포넌트 -->
<script>
export default {
methods: {
childMethod() {
console.log("자식 메소드 호출됨!");
},
updateData(data) {
this.formData = data;
}
}
};
</script>
Vue 3 Composition API
<!-- 부모 컴포넌트 (Vue 3) -->
<template>
<ChildComponent ref="childRef" />
<button @click="callChild">호출</button>
</template>
<script setup>
import { ref } from "vue";
import ChildComponent from "./ChildComponent.vue";
const childRef = ref(null);
const callChild = () => {
childRef.value.validateForm();
};
</script>
<!-- 자식 컴포넌트 -->
<script setup>
import { ref } from "vue";
const isValid = ref(false);
// 부모가 호출할 메소드를 expose
const validateForm = () => {
// 유효성 검사 로직
isValid.value = true;
return isValid.value;
};
// defineExpose로 외부 노출
defineExpose({
validateForm
});
</script>
자식에서 부모 메소드 호출 ($emit)
<!-- 부모 컴포넌트 -->
<template>
<ChildComponent
@child-event="handleChildEvent"
@update-data="updateParentData" />
</template>
<script>
export default {
methods: {
handleChildEvent(payload) {
console.log("자식에서 이벤트 발생:", payload);
},
updateParentData(data) {
this.parentData = data;
}
}
};
</script>
<!-- 자식 컴포넌트 -->
<template>
<button @click="notifyParent">부모에게 알림</button>
</template>
<script>
export default {
emits: ["child-event", "update-data"],
methods: {
notifyParent() {
// 이벤트 발생
this.$emit("child-event", { message: "Hello!" });
this.$emit("update-data", this.formData);
}
}
};
</script>
Vue 3 Composition API emit
<script setup>
// emit 정의
const emit = defineEmits(["submit", "cancel"]);
const handleSubmit = () => {
emit("submit", { name: "홍길동" });
};
const handleCancel = () => {
emit("cancel");
};
</script>
Props로 함수 전달
<!-- 부모 -->
<template>
<ChildComponent :on-save="handleSave" />
</template>
<script>
export default {
methods: {
handleSave(data) {
console.log("저장:", data);
}
}
};
</script>
<!-- 자식 -->
<template>
<button @click="onSave(formData)">저장</button>
</template>
<script>
export default {
props: {
onSave: {
type: Function,
required: true
}
}
};
</script>
provide/inject (깊은 중첩)
<!-- 조상 컴포넌트 -->
<script setup>
import { provide, ref } from "vue";
const showModal = ref(false);
const openModal = () => { showModal.value = true; };
const closeModal = () => { showModal.value = false; };
// 메소드 제공
provide("modalActions", { openModal, closeModal });
provide("showModal", showModal);
</script>
<!-- 깊은 자손 컴포넌트 -->
<script setup>
import { inject } from "vue";
const { openModal, closeModal } = inject("modalActions");
const showModal = inject("showModal");
</script>
이벤트 버스 대안 (mitt)
// eventBus.js
import mitt from "mitt";
export const emitter = mitt();
// 컴포넌트 A
import { emitter } from "./eventBus";
emitter.emit("user-login", { userId: 1 });
// 컴포넌트 B
import { emitter } from "./eventBus";
import { onMounted, onUnmounted } from "vue";
onMounted(() => {
emitter.on("user-login", handleLogin);
});
onUnmounted(() => {
emitter.off("user-login", handleLogin);
});
권장 패턴
- 부모→자식: ref 또는 props
- 자식→부모: emit 이벤트
- 깊은 중첩: provide/inject
- 형제/무관: Pinia 상태관리
javascript
| No | 작성일 | Title |
|---|---|---|
| 2921 | 2026. 05. 31. | 브라우저 중복 탭 작업 제어: BroadcastChannel과 Web Locks로 자동 저장 충돌 막기 |
| 2866 | 2026. 05. 23. | Node.js 운영 추적 로그 설계: AsyncLocalStorage로 요청 ID와 장애 원인 연결하기 |
| 2787 | 2026. 05. 15. | Node.js fetch 타임아웃·재시도 표준 패턴: AbortSignal로 장애 전파 막기 |
| 2589 | 2026. 04. 23. | ES2026 JavaScript 신기능 완전 가이드: using/await using, Array.fromAsync, Error.isError, Math.sumPrecise 실전 적용 |
| 2490 | 2026. 04. 14. | ES2025/ES2026 완벽 정리: Iterator Helpers, Temporal API, using 키워드 실전 가이드 |
| 2469 | 2026. 04. 13. | ES2026 완벽 가이드: Temporal API, using/await using, Array/Set 신기능 총정리 |
| 2449 | 2026. 04. 12. | ES2025/ES2026 JavaScript 신기능 완벽 정리: Temporal API부터 using 키워드까지 |
| 2425 | 2026. 04. 11. | Node.js 24 LTS 핵심 신기능 완벽 가이드 - V8 13.6, Permission Model, URLPattern |
| 2389 | 2026. 04. 09. | ES2025 핵심 신규 기능 총정리 - Set 연산, Iterator Helpers, Temporal API까지 |
| 2368 | 2026. 04. 08. | ES2025/ES2026 완벽 정리: Temporal API, Resource Management, 새 문법 총정리 |