CodexBloom - Programming Q&A Platform

Vue 3 Form Submit optimization guide with v-model on Array of Objects

👀 Views: 94 💬 Answers: 1 📅 Created: 2025-06-13
vue.js form v-model JavaScript

I’m working with an scenario with submitting a form in Vue 3 where I’m trying to bind an array of objects using v-model. I have a simple form that collects user input for multiple items, but when I try to submit the form, it doesn’t seem to capture the data correctly. Here’s a simplified version of my component: ```vue <template> <form @submit.prevent="handleSubmit"> <div v-for="(item, index) in items" :key="index"> <input v-model="item.name" placeholder="Item Name" /> <input v-model="item.quantity" type="number" placeholder="Quantity" /> <button type="button" @click="removeItem(index)">Remove</button> </div> <button type="button" @click="addItem">Add Item</button> <button type="submit">Submit</button> </form> </template> <script> export default { data() { return { items: [{ name: '', quantity: 1 }], }; }, methods: { addItem() { this.items.push({ name: '', quantity: 1 }); }, removeItem(index) { this.items.splice(index, 1); }, handleSubmit() { // This console log is empty console.log(this.items); // I'm expecting an array of objects with user inputs }, }, }; </script> ``` When I click the submit button, the console log inside `handleSubmit` shows an empty array: `[]`. I’ve confirmed that the data is being updated correctly in the inputs while typing. I’ve also checked for reactivity issues and ensured that Vue is not losing track of the data binding. I’ve tried using `v-model.lazy` in case there was an scenario with input events but that didn’t help. Additionally, I verified that the parent component isn’t affecting the state in an unexpected way. Is there something I’m missing with how Vue 3 handles form submissions or v-model in this context? I’m using Vue 3.0.0 and the browser console doesn’t show any errors. Any insights or suggestions would be appreciated.