Contents
see List작성일 2025. 11. 30.
Vue 3 Composition API 활용법
Vue 3의 Composition API를 사용하면 로직을 재사용 가능한 함수로 구성하고 더 유연한 컴포넌트를 작성할 수 있습니다.
Options API vs Composition API
// Options API (Vue 2 스타일)
export default {
data() { return { count: 0 } },
methods: { increment() { this.count++ } },
computed: { double() { return this.count * 2 } }
}
// Composition API (Vue 3)
import { ref, computed } from \047vue\047
export default {
setup() {
const count = ref(0)
const increment = () => count.value++
const double = computed(() => count.value * 2)
return { count, increment, double }
}
}script setup (권장)
<script setup>
import { ref, computed, onMounted } from \047vue\047
// 반응형 상태
const count = ref(0)
const user = ref({ name: \047홍길동\047 })
// 계산된 속성
const double = computed(() => count.value * 2)
// 메서드
const increment = () => count.value++
// 라이프사이클
onMounted(() => {
console.log(\047컴포넌트 마운트됨\047)
})
</script>
<template>
<button @click="increment">{{ count }}</button>
<p>Double: {{ double }}</p>
</template>ref vs reactive
import { ref, reactive } from \047vue\047
// ref: 원시값, 개별 값
const count = ref(0)
count.value++ // .value 필요
// reactive: 객체
const state = reactive({
count: 0,
user: { name: \047Kim\047 }
})
state.count++ // .value 불필요
// toRefs: reactive를 ref로 분해
const { count: countRef } = toRefs(state)Composables (로직 재사용)
// composables/useMouse.js
import { ref, onMounted, onUnmounted } from \047vue\047
export function useMouse() {
const x = ref(0)
const y = ref(0)
const update = (e) => {
x.value = e.pageX
y.value = e.pageY
}
onMounted(() => window.addEventListener(\047mousemove\047, update))
onUnmounted(() => window.removeEventListener(\047mousemove\047, update))
return { x, y }
}
// 컴포넌트에서 사용
<script setup>
import { useMouse } from \047./composables/useMouse\047
const { x, y } = useMouse()
</script>watch와 watchEffect
import { ref, watch, watchEffect } from \047vue\047
const count = ref(0)
const name = ref(\047\047)
// watch: 명시적 의존성
watch(count, (newVal, oldVal) => {
console.log(\047count changed:\047, newVal)
})
// 여러 소스 감시
watch([count, name], ([newCount, newName]) => {
console.log(newCount, newName)
})
// 깊은 감시
watch(user, (newVal) => {}, { deep: true })
// 즉시 실행
watch(count, handler, { immediate: true })
// watchEffect: 자동 의존성 추적
watchEffect(() => {
console.log(\047count is:\047, count.value)
})라이프사이클 훅
import {
onBeforeMount,
onMounted,
onBeforeUpdate,
onUpdated,
onBeforeUnmount,
onUnmounted
} from \047vue\047
onMounted(() => {
console.log(\047DOM 준비됨\047)
})
onUnmounted(() => {
console.log(\047정리 작업\047)
})Props와 Emits
<script setup>
// Props 정의
const props = defineProps({
title: String,
count: { type: Number, default: 0 }
})
// TypeScript
const props = defineProps<{
title: string
count?: number
}>()
// Emits 정의
const emit = defineEmits([\047update\047, \047delete\047])
emit(\047update\047, newValue)
</script>provide/inject
// 부모 컴포넌트
import { provide, ref } from \047vue\047
const theme = ref(\047dark\047)
provide(\047theme\047, theme)
// 자식 컴포넌트 (깊이 무관)
import { inject } from \047vue\047
const theme = inject(\047theme\047, \047light\047) // 기본값javascript
| No | 작성일 | Title |
|---|---|---|
| 3384 | 2026. 08. 21. | Node.js API에서 요청 취소를 제대로 처리하는 방법: AbortSignal로 외부 작업 정리하기 |
| 3352 | 2026. 08. 13. | Node.js 응답이 간헐적으로 느릴 때: 이벤트 루프 지연 측정과 CPU 작업 분리 방법 |
| 3320 | 2026. 08. 04. | JavaScript fetch 요청을 안전하게 만드는 방법: timeout·재시도·오류 처리 실전 패턴 |
| 3288 | 2026. 07. 27. | Node.js 서버 무중단 배포를 위한 Graceful Shutdown 구현 가이드 |
| 3257 | 2026. 07. 19. | Node.js 대용량 파일 처리 가이드: 스트림과 backpressure로 메모리 사용량 제어하기 |
| 3232 | 2026. 07. 11. | Node.js 실무형 에러 처리: async/await에서 실패를 일관되게 분류하고 복구 속도 높이기 |
| 3174 | 2026. 07. 02. | Node.js 환경 변수 검증 가이드: process.env를 안전한 설정 객체로 바꾸기 |
| 3123 | 2026. 06. 24. | TypeScript 타입 좁히기 운영 가이드: unknown 입력을 안전한 도메인 객체로 바꾸기 |
| 3065 | 2026. 06. 16. | 브라우저 성능 병목 찾기: PerformanceObserver로 LCP·INP·긴 작업 로그 수집하기 |
| 2978 | 2026. 06. 08. | Node.js fetch 타임아웃과 재시도 설계: 외부 API 장애 전파 줄이기 |