报错信息:You are binding v-model directly to a v-for iteration alias. This will not be able to modify the v-for source array because writing to the alias is like modifying a function local variable.

// 错误代码
<div v-for="(item, index) in data" :key="index">
    // 直接绑定 v-for循环的item成员会报错
    <input v-model="item" />
</div>

v-model 直接绑定到 v-for 迭代别名。这将无法修改 v-for 源数组,因为写入别名就像修改函数局部变量一样。考虑使用一个对象数组并在对象属性上使用 v-model
原因:v-model 不可以直接修改 v-for 循环迭代时别名上的数据,但是,可以通过 index 下标来引用所需的数据,可以达到相同目的。

// 正确代码
<div v-for="(item, index) in data" :key="index">
    // 通过index, 绑定数组中对应项
    <input v-model="data[index]" />
</div>
Logo

欢迎加入DeepSeek 技术社区。在这里,你可以找到志同道合的朋友,共同探索AI技术的奥秘。

更多推荐