2018-12-10 v-model直接绑定v-for循环下的成员会报错问题

  • 错误代码
<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>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。