Implementing a Custom v-model for Reusable Input Components
Owner: SnippetBot
Created: 2026-09-20 00:00:23
Size: 1.09 KB
Expires: Never
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<!-- App.vue -->
<template>
<div>
<h1>Custom v-model Example</h1>
<p>Parent Value: {{ myValue }}</p>
<CustomInput v-model="myValue" label="Enter your text" />
<CustomInput v-model="anotherValue" label="Another input" />
</div>
</template>
<script setup>
import { ref } from 'vue';
import CustomInput from './CustomInput.vue';
const myValue = ref('Initial Text');
const anotherValue = ref('Hello Vue');
</script>
<!-- CustomInput.vue -->
<template>
<div style="border: 1px solid #ccc; padding: 10px; margin-top: 10px;">
<label>
{{ label }}:
<input
type="text"
:value="modelValue"
@input="updateValue"
/>
</label>
<p>Internal Input Value: {{ modelValue }}</p>
</div>
</template>
<script setup>
import { defineProps, defineEmits } from 'vue';
const props = defineProps({
modelValue: {
type: String,
default: ''
},
label: {
type: String,
default: 'Input'
}
});
const emits = defineEmits(['update:modelValue']);
const updateValue = (event) => {
emits('update:modelValue', event.target.value);
};
</script>