App
Parent
ParentChild
<template>
<div>
<ParentChild my-msg='message' />
</div>
</template>
my-msg(prop이름) = "message" (prop 값)
<!--ParentChild.vue-->
<script setup>
defineProps(['myMsg'])
</script>
<!--ParentChild.vue-->
<script setup>
defineProps({
myMsg: String
})
</script>
<!--ParentChild.vue-->
<div>
<p>{{ myMsg }}</p>
</div>
<script setup>
const props = defineProps({ myMsg: String })
conole.log(props) // {myMsg:'message'}
console.log(props.myMsg) // message
<!--ParentChild.vue-->
<template>
<div>
<p>{{myMsg}}</p>
<ParentGrandChild :my-msg='myMsg' />
</div>
</template>
<!--ParentGrandChild.vue-->
<template>
<div>
<p>{{ myMsg }}</p>
</div>
</template>
<script setup>
defineProps({
myMsg: String,
})
</script>
<p>{{myMsg}}</p>
defineProps({
myMsg: String
})
<ParentChild my-msg='message' />
<!--Parent.vue-->
import { ref } from 'vue'
const name = ref('Alice')
<ParentChild my-msg='message' :dynamic-props='name' />
<!--ParentChild.vue-->
defineProps({
myMsg: String,
dynamicProps: String,
})
<p>{{ dynamicProps }}</p>
$emit()
: 자식 컴포넌트가 이벤트를 발생시켜 부모 컴포넌트로 데이터를 전달하는 역할의 메소드
$emit(event, ...args)
<button @click="$emit('someEvent')">클릭</button>
<ParentComp @some-event='someCallback' my-msg='message' :dynamic-props='name' />
const someCallback = function () {
console.log('ParentChild가 발생한 이벤트를 수신했어요')
}
<script setup>
const emit = defineEmits(['someEvent', 'myFocus'])
const buttonClick = function () {
emit('someEvent')
}
</script>
<!--ParentChild.vue-->
<script setup>
const emit = defineEmits(['someEvent'])
const buttonClick = function () {
emit('someEvent')
}
</script>
<button @click='buttonClick'>클릭</button>
이벤트 발신시 추가 인자를 전달해 값을 제공할 수 있다.
ParentChild에서 이벤트를 발신하여 Parent로 추가인자를 전달한다.
<!--ParentChild.vue-->
const emit = defineEmits(['someEvent', 'emitArgs'])
const emitArgs = function ) {
emit('emitArgs', 1,2,3)
}
<button @click='emitArgs'>추가 인자 전달</button>
<!--Parent.vue-->
<ParentChild
@some-event='someCallbakc'
@emit-args='getNumbers'
my-msg='message'
:dynamic-props='name'
/>
const
<!--ParentGrandChild.vue-->
const emit = defineEmits(['upadateName'])
const updateName = function() {
emit('updateName')
}
<!--ParentGrandChild.vue-->
<button @click='updateName'>이름 변경</button>
<!--ParentChild.vue-->
<ParentGrandChild
@update-name='updateName'
/>
<!--ParentChild.vue-->
const emit = defineEmits(['updateName'])
const updateName = function() {
emit('updateName')
}
<!--Parent.vue-->
<ParentChild @update-name='updateName'/>
<!--Parent.vue-->
const updateName = function() {
name.value = 'Bella'
}