// App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<MyComponent />
<HelloWorld msg="Welcome to Your Vue.js App"/>
</div>
</template>
msg=“~”
라는 property를 설정하였고, 하위 컴포넌트인 HelloWorld는 자신에게 부여된 msg property를 template에서 {{ msg }}
의 형태로 사용한 것// App.vue
<template>
<div id="app">
<img alt="Vue logo" src="./assets/logo.png">
<MyComponent />
<HelloWorld msg="App.vue에서 작성한 msg 입니다!"/>
</div>
</template>
prop-data-name=“value”
의 형태로 데이터를 전달// HelloWorld.vue
<script>
export default {
name: 'HelloWorld',
props: {
msg: String
}
}
</script>
// MyComponent.vue
<template>
<div class="border">
<h1>This is MyComponent</h1>
<MyChild static-props="component에서 child로"/>
</div>
</template>
// MyChild.vue
<template>
<div>
<h3>This is child component</h3>
<p>{{ staticProps }}</p>
</div>
</template>
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
}
}
</script>
kebab-case
(HTML 속성명은 대소문자를 구분하지 않기 때문)camelCase
// MyComponent.vue
<template>
<div class="border">
<h1>This is MyComponent</h1>
<MyChild
static-props="component에서 child로"
:dynamic-props="dynamicProps"
/>
</div>
</template>
<script>
import MyChild from '@/components/MyChild'
export default {
name: 'MyComponent',
components: {
MyChild,
},
data : function (){
return {
dynamicProps: "It's in data"
}
}
}
</script>
// MyChild.vue
<template>
<div>
<h3>This is child component</h3>
<p>{{ staticProps }}</p>
<p>{{ dynamicProps }}</p>
</div>
</template>
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
dynamicProps : String,
}
}
</script>
data : function () {
return {
// component's data in here
}
}
// MyComponent.vue
<template>
<div class="border">
<h1>This is MyComponent</h1>
<MyChild
static-props="component에서 child로"
:my-props="dynamicProps"
/>
</div>
</template>
<script>
import MyChild from '@/components/MyChild'
export default {
name: 'MyComponent',
components: {
MyChild,
},
data : function (){
return {
dynamicProps: "It's in data"
}
}
}
// MyChild.vue
<template>
<div>
<h3>This is child component</h3>
<p>{{ staticProps }}</p>
<p>{{ myProps }}</p>
</div>
</template>
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
myProps : String,
}
}
</script>
// 1 static props
<SomeComponent numprops="1"/>
// 2 dynamic props
<SomeComponent :numprops="1"/>
// 1 static props
<SomeComponent numprops="1"/>
// 2 dynamic props
<SomeComponent :numprops="'1'"/>
$emit(‘event-name’)
형식으로 사용하며 부모 컴포넌트에 event-name
이라는 이벤트가 발생했다는 것을 알림$emit(‘event-name’)
가 실행되면 event-name
이벤트가 발생하는 것[참고] $
$emit
을 통해 부모 컴포넌트에게 child-to-parent 이벤트를 트리거// MyChild.vue
<template>
<div>
<h3>This is child component</h3>
<p>{{ staticProps }}</p>
<p>{{ myProps }}</p>
<button @click="ChildToParent">클릭!</button>
</div>
</template>
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
myProps : String,
},
methods: {
ChildToParent : function(){
this.$emit('child-to-parent')
}
}
}
</script>
// MyComponent.vue
<template>
<div class="border">
<h1>This is MyComponent</h1>
<MyChild
static-props="component에서 child로"
:my-props="dynamicProps"
@child-to-parent="parentGetEvent"
/>
</div>
</template>
<script>
import MyChild from '@/components/MyChild'
export default {
name: 'MyComponent',
components: {
MyChild,
},
data : function (){
return {
dynamicProps: "It's in data"
}
},
methods: {
parentGetEvent: function(){
console.log("자식 컴포넌트에서 발생한 이벤트!")
}
}
}
</script>
// MyChild.vue
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
myProps : String,
},
methods: {
ChildToParent : function(){
this.$emit('child-to-parent', 'child Data')
}
}
}
</script>
// MyComponent.vue
<script>
import MyChild from '@/components/MyChild'
export default {
name: 'MyComponent',
components: {
MyChild,
},
data : function (){
return {
dynamicProps: "It's in data"
}
},
methods: {
parentGetEvent: function(inputData){
console.log("자식 컴포넌트에서 발생한 이벤트!")
console.log(`child에서 보낸 ${inputData} 보냄`)
}
}
}
</script>
~child data~
) 실행// MyChild.vue
<template>
<div>
<h3>This is child component</h3>
<p>{{ staticProps }}</p>
<p>{{ myProps }}</p>
<button @click="ChildToParent">클릭!</button>
<input type="text"
v-model="childInputData"
@keyup.enter="childInput"
>
</div>
</template>
<script>
export default {
name: 'MyChild',
props : {
staticProps : String,
myProps : String,
},
data: function () {
return {
childInputData : null,
}
},
methods: {
ChildToParent : function(){
this.$emit('child-to-parent', 'child Data')
},
childInput: function(){
this.$emit('child-input', this.childInputData)
this.childInputData = ""
}
}
}
</script>
// MyComponent.vue
<template>
<div class="border">
<h1>This is MyComponent</h1>
<MyChild
static-props="component에서 child로"
:my-props="dynamicProps"
@child-to-parent="parentGetEvent"
@child-input="getDynamicData"
/>
<!-- <MyChild
static-props="1"
:my-props="1"
/> -->
</div>
</template>
<script>
import MyChild from '@/components/MyChild'
export default {
name: 'MyComponent',
components: {
MyChild,
},
data : function (){
return {
dynamicProps: "It's in data"
}
},
methods: {
parentGetEvent: function(inputData){
console.log("자식 컴포넌트에서 발생한 이벤트!")
console.log(`child에서 보낸 ${inputData} 보냄`)
},
getDynamicData(input) {
console.log(`child로부터 받은 ${input}을 출력`)
}
}
}
</script>
~입력받은 데이터~
) 실행https://github.com/mjieun0956/TIL/tree/master/Vue/06.%20Vue%20Data%20Management%20(props%2C%20emit)