美文网首页vue.js
2018-12-10 v-model直接绑定v-for循环下的成

2018-12-10 v-model直接绑定v-for循环下的成

作者: 土豆切成片 | 来源:发表于2018-12-10 18:04 被阅读0次
  • 错误代码
<div v-for="(item, index) in data" :key="index">
  // 直接绑定 v-for循环的item成员会报错
  <input v-model="item" />
</div>
  • 报错

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. Consider using an array o

报错内容为: 你将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>

相关文章

网友评论

    本文标题:2018-12-10 v-model直接绑定v-for循环下的成

    本文链接:https://www.haomeiwen.com/subject/rdedhqtx.html