Last active
July 4, 2023 09:00
-
-
Save Haerezis/e1737f113ee86fe109c39ff113fd4656 to your computer and use it in GitHub Desktop.
VModelProxy.vue (vuejs 3)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<template> | |
<label> | |
VModelProxy | |
<input type="text" v-model="localValue"> | |
</label> | |
</template> | |
<script setup> | |
import { ref, computed, watch } from 'vue' | |
const props = defineProps({ | |
modelValue: String | |
}) | |
const emit = defineEmits(['update:modelValue']) | |
const _localValue = ref(props.modelValue) | |
const localValue = computed({ | |
get: () => _localValue.value, | |
set: (newValue) => { | |
_localValue.value = newValue | |
emit("update:modelValue", newValue) | |
} | |
}) | |
watch( | |
() => props.modelValue, | |
(newValue) => _localValue.value = newValue | |
) | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment