关于在Vue的v-for循环中使用v-model的技术咨询及代码示例
Hey there! Let’s break down your Vue code and cover best practices for using v-model within a v-for loop for your companion form fields.
First, let’s clean up your existing code snippet for clarity:
<div v-for="(companion, key) in planForm.companions" :key="key"> <div class="field"> <label class="label">First Name</label> <div class="input-space"> <input :name="'companion_first_name[' + key + ']'" type="text" v-model="companion.first_name"> </div> </div> <div class="field"> <label class="label">Last Name</label> <div class="input-space"> <input :name="'companion_last_name[' + key + ']'" type="text" v-model="companion.last_name"> </div> </div> </div>
What’s working well here:
- You’re correctly iterating over the
planForm.companionsarray withv-for, and bindingv-modeldirectly to each companion’s properties—this will keep your form inputs synced with your component’s data state perfectly. - The dynamic
nameattribute setup makes sense for form submission, as it will send data in a structured array format.
Quick optimizations & best practices:
Simplify the
nameattribute with template literals
Ditch string concatenation for cleaner, more readable ES6 template literals:<input :name="`companion_first_name[${key}]`" type="text" v-model="companion.first_name">Use a unique
keyinstead of the index (when possible)
If each companion has a unique identifier (like anid), use that for the:keyprop instead of the array index. This helps Vue track list items more efficiently, especially when adding/removing companions (avoids weird input value mismatches):<div v-for="(companion, key) in planForm.companions" :key="companion.id">Ensure
planForm.companionsis properly initialized
In your component’s data, initialize the array with at least one empty object (or as many as needed) to avoid reactivity issues:data() { return { planForm: { companions: [ { first_name: '', last_name: '' } ] } } }Add new companions reactively
When adding a new companion to the array:- For Vue 2: Use
this.$setto ensure the new object is reactive (Vue 2 can’t detect plain object additions to arrays automatically):addCompanion() { this.$set(this.planForm.companions, this.planForm.companions.length, { first_name: '', last_name: '' }); } - For Vue 3: You can just push the object directly—Vue 3’s reactivity system handles this seamlessly:
addCompanion() { this.planForm.companions.push({ first_name: '', last_name: '' }); }
- For Vue 2: Use
Common pitfalls to avoid:
- Forgetting the
:keyprop: This can cause unexpected behavior when modifying the list (e.g., input values sticking to the wrong list items). - Initializing
companionsas an object instead of array:v-forwon’t iterate over an object correctly for this use case—stick to an array. - Ignoring reactivity in Vue 2: If you push a plain object without
$set, Vue won’t track changes to its properties, sov-modelwon’t update the data as expected.
That should cover everything you need to make this form setup work smoothly. If you run into specific issues like submission errors or reactivity bugs, feel free to dive deeper!
内容的提问来源于stack exchange,提问作者Mango D

