프론트엔드/Vue

Vue에서 defineModel로 컴포넌트 간 양방향 바인딩하기

dig04214 2025. 3. 24. 17:04

Vue 3.4부터는 defineModel() 매크로가 추가되었다.

덕분에 양방향 바인딩이 편해졌다.

<!-- Child.vue -->
<script setup>
const inputValue = defineModel()
</script>

<template>
    <div>
        <label for="test">
            test
        </label>
        <input type="text"v-model="inputValue">
    </div>
</template>

<style scoped></style>
<!-- Parent.vue -->
<script setup>
import { ref } from 'vue'
import Child from './components/Child.vue' 

const value = ref('')
</script>

<template>
    <Child v-model="value" />
</template>

<style scoped>

</style>

 

이런 식으로 defineModel 매크로를 사용하면, defineProps, defineEmit 없이 컴포넌트 간 양방향 바인딩이 가능해진다.

'프론트엔드 > Vue' 카테고리의 다른 글

Vue에서 사용할 수 있는 예쁜 테이블, AG Grid  (0) 2025.03.26
Vue에서 props는 ReadOnly이다  (0) 2025.03.24