
부모 컴포넌트에서만 변경하고 이를 내려받는 자식 컴포넌트는 자연스럽게 갱신
데이터 흐름의 "일관성" 및 "단순화"
ParentChild.vue
<script setup>
defineProps(['myMsg'])
</script>
ParentChild.vue
<script setup>
defineProps({
myMsg: String
})
</script>
ParentChild.vue
<div>
<p>{{ myMsg }}</p>
</div>
- props를 객체로 반환하므로 필요한 경우 JavaScript에서 접근 가능
```html
<script setup>
const props = defineProps({ myMsg: String })
console.log(props) // {myMsg: 'message'}
console.log(props.myMsg) // 'message'
</script>
import './assets/main.css'App.vue
<template>
<div>
<Parent />
</div>
</template>
<script setup>
import Parent from '@/components/Parent.vue'
</script>
Parent.vue
<template>
<div>
<ParentChild />
</div>
</template>
<script setup>
import ParentChild from '@/components/ParentChild.vue'
</script>
ParentChild.vue
<template>
<div></div>
</template>
<script setup>
</script>
my-msg = "message"Parent..vue
<template>
<div>
<ParentChild my-msg="message">
</div>
</template>
<ParentChild my-msg="message" />defineProps({
myMsg: String
)}
<p>{{ myMsg }}</p>
2-1. Dynamic props 정의
Parent.vue
js
import { ref } from 'vue'
const name = ref('Alice')
html
<ParentChild my-msg="message" :dynamic-props="name" />
2-2. Dynamic props 선언 및 출력
ParentChild.vue
js
defineProps({
myMsg: String,
dynamicProps: String,
})
html
<p>{{ dynamicProps }}</p>
ParentItem.vue
<template>
<div>
<p>{{ myProp }}</p>
</div>
</template>
<script setup>
defineProps({
myProp: Object,
})
</script>
<style scoped></style>
Parent.vue
<template>
<div>
<ParentChild my-msg="message" :dynamic-props="name" />
<ParentItem v-for="item in items" :key="item - id" :my-prop="item" />
</div>
</template>
<script setup>
import { ref } from 'vue'
import ParentChild from '@/components/ParentChild.vue'
import ParentItem from '@/components/ParentItem.vue'
const items = ref([
{ id: 1, name: '사과' },
{ id: 2, name: '바나나' },
{ id: 3, name: '딸기' },
])
const name = ref('Alice')
</script>
<style scoped></style>
부모가 props 데이터를 변경하도록 소리쳐야 함
$ 표기는 Vue 인스턴스의 내부 변수들을 가리킴$emit(event, ...args)
$emit을 사용하여 템플릿 표현식에서 직접 사용자 정의 이벤트를 발신button @click="$emit('someEvent')">클릭</button>
<ParentComp @some-event="someCallback" />
ParentChild.vue<button @click="$emit('someEvent')">클릭</button>
Parent.vue
<ParentChild @some-event="someCallback" my-msg="message" :dynamic-props="name" />
Parent.vue
const someCallback = function () {
console.log('ParentChild가 발신한 이벤트를 수신했어요.')
}
js
<script setup>
defineEmits()
</script>
js
<script setup>
const emit = defineEmits(['someEvent', 'myFocus'])
const buttonClick = function () {
emit('someEvent')
}
</script>
ParentChild.vue
js
<script setup>
const emit = defineEmits(['someEvent'])
const buttonClick = function () {
emit('someEvent')
}
</script>
html
<button @click="buttonClick">클릭</button>
이벤트 인자
이벤타 발신 시 추가 인자를 전달하여 값을 제공할 수 있음
ParentChild에서 이벤트를 발신하여 Parent로 추가 인자 전달하기
ParentChild에서 발신한 이벤트를 Parent에서 수신
html
<ParentChild my-msg="message"
:dynamic-props="name"
@some-event="someCallback"
@my-focus="someCallback2"
@emit-args="getNumbers"/>
js
const getNumbers = function (...args) {
console.log(args)
}
html
<button @click="$emit('someEvent')">클릭</butotn>
js
const emit = defineEmits(['someEvent'])
emit('someEvent')
html
<ParentChild @some-event="..." />
ParentGrandChild.vue
js
const emit = defineEmits(['updateName'])
const updateName = function () {
emit('updateName')
}
html
<button @click="updateName">이름 변경</button>
ParentChild.vue
js
const emit = defineEmits(['someEvent, 'emitArgs', updateName'])
const updateName = function () {
emit('updateName')
}
html
<ParentGrandChild :my-msg="myMsg" @update-name="updateName" />
Parent.vue
<ParentChild @update-name="updateName" />
const updatename = function () {
name.value = 'Bella'
}
<SomeComponent num-props="1" />
<SomeComponent :num-props="1' />
defineProps({
// 여러 타입 허용
propB: [String, Number],
// 문자열 필수
propC: {
type: String,
required: true
},
// 기본 값을 가지는 숫자형
propD: {
type: Number,
default: 10
},
...
const emit = defineEmits({
// 유효성 검사 없음
click: null,
// submit 이벤트 유효성 검사
submit: ({email, password }) => {
if (email && password) {
return true
} else {
console.warn('submit 이벤트가 옳지 않음')
return false
}
}
})
const submitForm = function (email, password) {
emit('submit', { email, password })
}