Emit Event
Emit Event ๊ฐ์
โ ๋ถ๋ชจ ์ปดํฌ๋ํธ์์ ์์ ์ปดํฌ๋ํธ๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌํ ๋๋ ์ด๋ฒคํธ๋ฅผ ๋ฐ์ ์ํจ๋ค.
- ๋ฐ์ดํฐ๋ฅผ ์ด๋ฒคํธ ๋ฆฌ์คํฐ์ ์ฝ๋ฐฑํจ์ ์ธ์๋ก ์ ๋ฌ
- ์์ ์ปดํฌ๋ํธ๋ ํด๋น ์ด๋ฒคํธ๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฅผ ๋ฐ์
Emit Event ๊ธฐ์ด
<!-- MyComponentItem.vue -->
<template>
<div>
...
<button @click="childToParent">ํด๋ฆญ!</button>
</div>
</template>
<script>
export default {
...
methods: {
childToParent: function () {
this.$emit("child-to-parent");
},
},
};
</script>
<style></style>
<!-- MyComponent.vue -->
<template>
<div class="border">
<h1>This is my Component</h1>
<MyComponentItem ... @child-to-parent="parentGetEvent" />
</div>
</template>
<script>
...
export default {
...
methods: {
parentGetEvent: function () {
console.log("event from child!");
},
},
};
</script>
ํ๋ฆ ์ ๋ฆฌ
- ์์ ์ปดํฌ๋ํธ์ ์๋ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(
childToParent
) ํธ์ถ - ํธ์ถ๋ ํจ์์์
$emit
์ ํตํด ์์ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ(child-to-parent
) ๋ฐ์ - ์์ ์ปดํฌ๋ํธ๋ ์์ ์ปดํฌ๋ํธ๊ฐ ๋ฐ์์ํจ ์ด๋ฒคํธ(
child-to-parent
)๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(parentGetEvent
) ํธ์ถ
Emit with Data
<!-- MyComponentItem.vue -->
<template>
<div>
...
<button @click="childToParent">ํด๋ฆญ!</button>
</div>
</template>
<script>
export default {
...
methods: {
childToParent: function () {
this.$emit("child-to-parent", "child data");
},
},
};
</script>
<style></style>
โ ์ด๋ฒคํธ๋ฅผ ๋ฐ์(emit) ์ํฌ ๋ ์ธ์๋ก ๋ฐ์ดํฐ๋ฅผ ์ ๋ฌ!
<!-- MyComponent.vue -->
<template>
<div class="border">
<h1>This is my Component</h1>
<MyComponentItem ... @child-to-parent="parentGetEvent" />
</div>
</template>
<script>
...
export default {
...
methods: {
parentGetEvent: function (inputData) {
console.log("event from child!");
console.log(`get ${inputData} from child component ๋ฐ์`);
},
},
};
</script>
โ ์ ๋ฌํ ๋ฐ์ดํฐ๋ ์ด๋ฒคํธ์ ์ฐ๊ฒฐ๋ ๋ถ๋ชจ ์ปดํผ๋ํธ์ ํธ๋ค๋ฌ ํจ์์ ์ธ์๋ก ์ฌ์ฉ ๊ฐ๋ฅ
ํ๋ฆ ์ ๋ฆฌ
- ์์ ์ปดํฌ๋ํธ์ ์๋ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(
ChildToParent
)ํธ์ถ - ํธ์ถ๋ ํจ์์์
$emit
์ ํตํด ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ(child-to-parent
)๋ฅผ ๋ฐ์ (์ด๋ฒคํธ์ ๋ฐ์ดํฐ(child data
)ํจ๊ป ์ ๋ฌ) - ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ ์์ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ(
child-to-parent
)๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(parentGetEvent
)ํธ์ถ, ํจ์์ ์ธ์๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ(child data
)๊ฐ ํฌํจ - ํธ์ถ๋ ํจ์์์
console.log(~child data~)
์คํ
emit dynamic data
<!-- MyComponentItem.vue -->
<template>
<div>
...
<button @click="childInput">ํด๋ฆญ!</button>
<input type="text" v-model="childInputData" />
</div>
</template>
<script>
export default {
...
methods: {
childInput: function () {
this.$emit("child-input", this.childInputData);
},
},
};
</script>
<style></style>
<!-- MyComponent.vue -->
<template>
<div class="border">
<h1>This is my Component</h1>
<MyComponentItem ... @child-input="getDynamicData" />
</div>
</template>
<script>
...
export default {
...
data: function () {
return {
dynamicProps: "It's in data",
};
},
methods: {
getDynamicData: function (inputData) {
console.log(12);
console.log(`get dynamic ${inputData} from child component`);
},
},
};
</script>
ํ๋ฆ ์ ๋ฆฌ
- ์์ ์ปดํฌ๋ํธ์ ์๋ ๋ฒํผ ํด๋ฆญ ์ด๋ฒคํธ๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(
ChildInput
)ํธ์ถ - ํธ์ถ๋ ํจ์์์
$emit
์ ํตํด ๋ถ๋ชจ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ(child-input
)๋ฅผ ๋ฐ์ (์ด๋ฒคํธ์ v-modle๋ก ๋ฐ์ธ๋ฉ ๋ ์ ๋ ฅ๋ฐ์ ๋ฐ์ดํฐ ์ ๋ฌ) - ๋ถ๋ชจ ์ปดํฌ๋ํธ๋ ์์ ์ปดํฌ๋ํธ์ ์ด๋ฒคํธ(
child-input
)๋ฅผ ์ฒญ์ทจํ์ฌ ์ฐ๊ฒฐ๋ ํธ๋ค๋ฌ ํจ์(getDynamicData
)ํธ์ถ, ํจ์์ ์ธ์๋ก ์ ๋ฌ๋ ๋ฐ์ดํฐ๊ฐ ํฌํจ - ํธ์ถ๋ ํจ์์์
console.log(~์ ๋ ฅ๋ฐ์ ๋ฐ์ดํฐ~)
์คํ
pass props / emit event ์ปจ๋ฒค์
โ HTML ์์์์ ์ฌ์ฉํ ๋๋ kebab-case
โ JavaScript์์ ์ฌ์ฉํ ๋๋ camelCase
โ props
- ์์ -> ํ์ ํ๋ฆ์์ HTML ์์๋ก ๋ด๋ ค์ค ๋: kebab-case
- ํ์์์ ๋ฐ์ ๋ JavaScript์์ ๋ฐ์: camelCase
โ emit
- emit ์ด๋ฒคํธ๋ฅผ ๋ฐ์์ํค๋ฉด HTML ์์๊ฐ ์ด๋ฒคํธ๋ฅผ ์ฒญ์ทจ: kebab-case
- ๋ฉ์๋, ๋ณ์๋ช ๋ฑ์ JavaScript์์ ์ฌ์ฉ: camelCase
'โญ Personal_Study > Vue' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Lifecycle Hooks (0) | 2022.11.14 |
---|---|
Vuex (0) | 2022.11.14 |
Pass Props (0) | 2022.11.12 |
Vue Component (0) | 2022.11.11 |
SFC (Single File Component) (0) | 2022.11.11 |
๋๊ธ