Vue
Vue 3.5 기반. 항상 <script setup lang="ts">와 함께 Composition API를 사용하세요.
선호 사항
- JavaScript보다 TypeScript 선호
<script>보다 <script setup lang="ts"> 선호
- 성능을 위해 깊은 반응성이 필요하지 않은 경우
ref보다 shallowRef 선호
- 항상 Options API보다 Composition API 사용
- 반응형 Props 구조 분해(Reactive Props Destructure) 사용 권장하지 않음
핵심
| 주제 | 설명 | 참고 문서 |
|---|
| Script Setup 및 매크로 | <script setup>, defineProps, defineEmits, defineModel, defineExpose, defineOptions, defineSlots, 제네릭 | script-setup-macros |
| 반응형 및 생명주기 | ref, shallowRef, computed, watch, watchEffect, effectScope, 생명주기 훅, 컴포저블(composables) | core-new-apis |
기능
| 주제 | 설명 | 참고 문서 |
|---|
| 내장 컴포넌트 및 지시문 | Transition, Teleport, Suspense, KeepAlive, v-memo, 사용자 정의 지시문 | advanced-patterns |
빠른 참조
컴포넌트 템플릿
<script setup lang="ts">
import { ref, computed, watch, onMounted } from 'vue'
const props = defineProps<{
title: string
count?: number
}>()
const emit = defineEmits<{
update: [value: string]
}>()
const model = defineModel<string>()
const doubled = computed(() => (props.count ?? 0) * 2)
watch(() => props.title, (newVal) => {
console.log('Title changed:', newVal)
})
onMounted(() => {
console.log('Component mounted')
})
</script>
<template>
<div>{{ title }} - {{ doubled }}</div>
</template>
주요 임포트
import { ref, shallowRef, computed, reactive, readonly, toRef, toRefs, toValue } from 'vue'
import { watch, watchEffect, watchPostEffect, onWatcherCleanup } from 'vue'
import { onMounted, onUpdated, onUnmounted, onBeforeMount, onBeforeUpdate, onBeforeUnmount } from 'vue'
import { nextTick, defineComponent, defineAsyncComponent } from 'vue'