Build a form component in Vue that handles input changes and submission.
Code Playground
JavaScript (V8)
Loading...
Console Output
// Output will appear here...
// For React: ReactDOM.createRoot(rootElement).render(<App />);
// For React: ReactDOM.createRoot(rootElement).render(<App />);
Recommended Answer
Answer using v-model for two-way data binding:
<template>
<form @submit.prevent="handleSubmit">
<label>
Name:
<input type="text" v-model="inputValue" />
</label>
<button type="submit">Submit</button>
</form>
</template>
<script setup>
import { ref } from 'vue';
const inputValue = ref('');
const handleSubmit = () => {
alert(`Submitted value: ${inputValue.value}`);
inputValue.value = ''; // Reset input
};
</script>