Trung BìnhVue.js iconVue.js

v-model hoạt động thế nào? Custom v-model?

v-model là shorthand: :modelValue + @update:modelValue.

Mặc định cho input:

vue
<!-- Tương đương -->
<input v-model="name" />
<input :value="name" @input="name = $event.target.value" />

Custom component v-model:

vue
<script setup>
// Child component
const props = defineProps(['modelValue'])
const emit = defineEmits(['update:modelValue'])
// Template: :value="modelValue" @input="emit('update:modelValue', $event)"
</script>

Vue 3 hỗ trợ multiple v-model: v-model:title, v-model:content với props tương ứng.

Xem toàn bộ Vue.js cùng filter theo level & chủ đề con.

Mở danh sách Vue.js