Programming/Vue

[Vue .js] Composition API : props, emits ์‚ฌ์šฉ๋ฒ•

Space_Jin 2023. 12. 16. 16:44
728x90
๋ฐ˜์‘ํ˜•

composition API porps, emits ์‚ฌ์šฉ๋ฒ•

 

Vue์— ์ถ”๊ฐ€๋œ composition API๋Š” Vue2์—์„œ์˜ option API๋ณด๋‹ค ์ปดํฌ๋„ŒํŠธ๋ฅผ ์กฐ๊ธˆ ๋” ๋…๋ฆฝ์ ์œผ๋กœ ๋งŒ๋“ค๊ธฐ ์œ ๋ฆฌํ•˜๋‹ค.

๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ(์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฐ์ดํ„ฐ ์ปจํ…Œ์ด๋„ˆ ์—ญํ• ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ง์ ‘ ์กฐ์ž‘, ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์กฐ์ž‘ํ•˜์ง€ ์•Š๊ณ  ํ‘œํ˜„๋งŒ์„ ๋‹ด๋‹น)๋ฅผ ํ†ตํ•ด ์ปดํฌ๋„ŒํŠธ์˜ ์žฌ์‚ฌ์šฉ๋ฅ ์„ ๋†’์ผ ์ˆ˜ ์žˆ๋‹ค.

props์™€ emits ์‚ฌ์šฉ๋ฒ• ์˜ˆ์‹œ

์•„๋ž˜์˜ˆ์‹œ๋Š” App.vue ์•ˆ์—์„œ TodoList.vue ์ปดํฌ๋„ŒํŠธ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

App.vue

<template>
  <div id="app">
    <TodoList :todoItemProps="todoItems" @emitClickList="alertList"></TodoList>    
  </div>
</template>

<script setup>
import TodoList from './components/TodoList.vue'
import { ref } from 'vue';

const todoItems = ref(['itemOne', 'itemTwo', 'itemThree']);

const alertList = (idx) => {
  alert(todoItems.value[idx]);
}
</script>

์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์ธ App.vue์—์„œ ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ datadls props๋ฅผ ๋‚ด๋ ค์ฃผ๊ณ  ํ•˜์œ„ ์ปดํฌ๋„ŒํŠธ์—๊ฒŒ ์ด๋ฒคํŠธ์ธ emits์„ ๋ฐ›๋Š”๋‹ค.

 

ํ•˜์œ„์ปดํฌ๋„ŒํŠธ(TodoList.vue)์˜ todoItemsProps๋ž€ ์ด๋ฆ„์˜ props์— ์ƒ์œ„์ปดํฌ๋„ŒํŠธ(App.vue)์˜ todoItems๋ผ๋Š” ๋ฐฐ์—ด์„ ๋‚ด๋ฆฐ๋‹ค.

 

ํ•˜์œ„์ปดํฌ๋„ŒํŠธ(TodoList.vue)์˜ emitClickList๋ž€ ์ด๋ฆ„์˜ ์ด๋ฒคํŠธ์— ์ƒ์œ„์ปดํฌ๋„ŒํŠธ(App.vue)์˜ alertList ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ๋‹ค.

 

TodoList.vue

<template>
    <div>
        <ul>
            <li v-for="(todoItem, idx) in props.todoItemProps" v-bind:key="todoItem">
                <span>{{ todoItem.item }}</span>
                <span @click="clickList(idx)">Alert</span>
            </li>
        </ul>
    </div>
</template>

<script setup>
import { defineProps, defineEmits } from 'vue';

const props = defineProps({
	//์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ todoItemsProps๋ผ๋Š” ์ด๋ฆ„์˜ props๋ฅผ ์„ ์–ธ(type์€ ๋ฐฐ์—ด)
	//type => ๋ฌธ์ž์—ด: String, ์ˆซ์žํ˜•: Number, ๋ฐฐ์—ด: Array, ๋…ผ๋ฆฌํ˜•: Boolean
	todoItemProps: Array
})

const emit = defineEmits(['emitClickList'])

const clickList = (idx) => {
    //idx ์ธ์ž๋ฅผ emitClickList ๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์ „๋‹ฌ
    emit('emitClickList', idx); 
}

</script>

1. props, emit์€ ๋ชจ๋‘ ๊ฐ์ฒด๋กœ defineProps, defineEmits ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜์—ฌ ์„ ์–ธ ๋ฐ ์ •์˜ํ•œ๋‹ค.

2. props์—๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋ฐ›์€ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ์ •์˜ํ•œ๋‹ค.

3. emit์—๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ๋„˜๊ฒจ์ค„ emit๋“ค์„ ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•œ๋‹ค.

 

todoItemProps ์•ˆ์—๋Š” ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ๋„˜๊ฒจ์ค€ todoItems ๋ผ๋Š” ๋ฐฐ์—ด์ด ๋“ค์–ด์žˆ๋‹ค.

๋ฐฐ์—ด์„ ๊บผ๋‚ด์“ธ ๋•Œ๋Š” in ํ‘œํ˜„์‹์„ ์ด์šฉํ•ด์„œ ๋ฐฐ์—ด ์•ˆ์˜ ๋ฐ์ดํ„ฐ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. (์•„์ดํ…œ, ์ธ๋ฑ์Šค) in ๋ฐฐ์—ด๊ฐ’

์•„์ดํ…œ, ์ธ๋ฑ์Šค ์ˆœ์„œ๋กœ ์ œ๊ณต๋˜๊ณ  ์ด๋ฆ„์€ ์ž„์˜๋กœ ์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

@click์„ ํ†ตํ•ด์„œ ํด๋ฆญ ์ด๋ฒคํŠธ๊ฐ€ ์ผ์–ด๋‚ฌ์„๋•Œ clickList ํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋˜๊ณ  ํ•ด๋‹น ํ•จ์ˆ˜๋Š” emitClickList๋ผ๋Š” ์ด๋ฆ„์œผ๋กœ ์ธ์ž์ธ idx๋ฅผ ์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์— ์˜ฌ๋ ค์ค€๋‹ค.

์ƒ์œ„ ์ปดํฌ๋„ŒํŠธ์—์„œ ํ•ด๋‹น emit์ด ์ผ์–ด๋‚ฌ์„ ๋•Œ, ์ˆ˜ํ–‰๋  ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•