๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
โญ Personal_Study/Vue

Emit Event

by ํฌ์ŠคํŠธ์‰์ดํฌ 2022. 11. 13.

Emit Event

Emit Event ๊ฐœ์š”

โœ” ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์—์„œ ์ž์‹ ์ปดํฌ๋„ŒํŠธ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ „๋‹ฌํ•  ๋•Œ๋Š” ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ ์‹œํ‚จ๋‹ค.

  1. ๋ฐ์ดํ„ฐ๋ฅผ ์ด๋ฒคํŠธ ๋ฆฌ์Šคํ„ฐ์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜ ์ธ์ž๋กœ ์ „๋‹ฌ
  2. ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ํ•ด๋‹น ์ด๋ฒคํŠธ๋ฅผ ํ†ตํ•ด ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›์Œ

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>

ํ๋ฆ„ ์ •๋ฆฌ

  1. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒญ์ทจํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜(childToParent) ํ˜ธ์ถœ
  2. ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ $emit์„ ํ†ตํ•ด ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ(child-to-parent) ๋ฐœ์ƒ
  3. ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ๊ฐ€ ๋ฐœ์ƒ์‹œํ‚จ ์ด๋ฒคํŠธ(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>

โœ” ์ „๋‹ฌํ•œ ๋ฐ์ดํ„ฐ๋Š” ์ด๋ฒคํŠธ์™€ ์—ฐ๊ฒฐ๋œ ๋ถ€๋ชจ ์ปดํผ๋„ŒํŠธ์˜ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์‚ฌ์šฉ ๊ฐ€๋Šฅ

ํ๋ฆ„ ์ •๋ฆฌ

  1. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒญ์ทจํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜(ChildToParent)ํ˜ธ์ถœ
  2. ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ $emit์„ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ(child-to-parent)๋ฅผ ๋ฐœ์ƒ (์ด๋ฒคํŠธ์— ๋ฐ์ดํ„ฐ(child data)ํ•จ๊ป˜ ์ „๋‹ฌ)
  3. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฒคํŠธ(child-to-parent)๋ฅผ ์ฒญ์ทจํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜(parentGetEvent)ํ˜ธ์ถœ, ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ(child data)๊ฐ€ ํฌํ•จ
  4. ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ 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>

ํ๋ฆ„ ์ •๋ฆฌ

  1. ์ž์‹ ์ปดํฌ๋„ŒํŠธ์— ์žˆ๋Š” ๋ฒ„ํŠผ ํด๋ฆญ ์ด๋ฒคํŠธ๋ฅผ ์ฒญ์ทจํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜(ChildInput)ํ˜ธ์ถœ
  2. ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ $emit์„ ํ†ตํ•ด ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ์— ์ด๋ฒคํŠธ(child-input)๋ฅผ ๋ฐœ์ƒ (์ด๋ฒคํŠธ์— v-modle๋กœ ๋ฐ”์ธ๋”ฉ ๋œ ์ž…๋ ฅ๋ฐ›์€ ๋ฐ์ดํ„ฐ ์ „๋‹ฌ)
  3. ๋ถ€๋ชจ ์ปดํฌ๋„ŒํŠธ๋Š” ์ž์‹ ์ปดํฌ๋„ŒํŠธ์˜ ์ด๋ฒคํŠธ(child-input)๋ฅผ ์ฒญ์ทจํ•˜์—ฌ ์—ฐ๊ฒฐ๋œ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜(getDynamicData)ํ˜ธ์ถœ, ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ ์ „๋‹ฌ๋œ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ
  4. ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ 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

๋Œ“๊ธ€