简体   繁体   English

在数组和多个文本输入之间在 vue.js 中创建双向绑定

[英]Create a two-way binding in vue.js between an array and multiple text inputs

My data is stored in an array.我的数据存储在一个数组中。 For each array item, there should be a text input in the form.对于每个数组项,表单中应该有一个文本输入。 When the user types into one of the text inputs, the array should be updated with the new values.当用户键入文本输入之一时,应使用新值更新数组。

  <div class="form-group" v-for="synonym in row.synonyms">
    <input type="text" class="form-control" v-model="synonym" />
  </div>

Here's a fiddle: https://jsfiddle.net/eywraw8t/122210/这是一个小提琴: https ://jsfiddle.net/eywraw8t/122210/

The idea is when you type into one of the textboxes, the array value (shown below in that fiddle) should also update, but it doesn't.这个想法是当您输入其中一个文本框时,数组值(如下所示)也应该更新,但它不会。

Upon inspecting the console, you would find the following error:检查控制台后,您会发现以下错误:

You are binding v-model directly to a v-for iteration alias.您将 v-model 直接绑定到 v-for 迭代别名。 This will not be able to modify the v-for source array because writing to the alias is like modifying a function local variable.这将无法修改 v-for 源数组,因为写入别名就像修改函数局部变量一样。 Consider using an array of objects and use v-model on an object property instead.考虑使用对象数组并在对象属性上使用 v-model 。

Meaning, we need to give v-model access to a direct reference to the synonym and its index:这意味着,我们需要让v-model访问对同义词及其索引的直接引用:

 new Vue({ el: "#app", data: { row: { synonyms: [ "abc", "def", "ghj", ] } }, methods: { } })
 body { font-family: 'Exo 2', sans-serif; } #app { margin: auto; }
 <div id="app"> <h2>Items</h2> <div class="form-group" v-for="(synonym,i) in row.synonyms"> <input type="text" class="form-control" v-model="row.synonyms[i]" /> </div> <br> <h3> The text below should change if yout type inside the textboxes: </h3> <p> {{ JSON.stringify(row)}} </p> </div> <script src="https://cdn.jsdelivr.net/npm/vue@2.5.16/dist/vue.js"></script>

The correct way to do it is to use an index, which vue.js provides in loops:正确的做法是使用索引,vue.js 在循环中提供:

  <div class="form-group" v-for="(synonym, index) in row.synonyms">
    <input type="text" class="form-control" v-model="row.synonyms[index]" />
  </div>

https://jsfiddle.net/m14vd89u/1/ https://jsfiddle.net/m14vd89u/1/

This is the recommended way that Vue.js wants you to do it by using an index (synonym, index) :这是 Vue.js 希望您通过使用索引(synonym, index)来做到这一点的推荐方式:

https://v2.vuejs.org/v2/guide/list.html https://v2.vuejs.org/v2/guide/list.html

<div class="form-group" v-for="(synonym, index) in row.synonyms">
  <input type="text" class="form-control" v-on:blur="onItemsChanged(synonym)" v-model="row.synonyms[index]" />
</div>

If you wanted to do it another way you could introduce a method v-on:blur :如果你想用另一种方式来做,你可以引入一个方法v-on:blur

new Vue({
  el: "#app",
  data: {
    row: {
        synonyms: [
        "abc", 
        "def", 
        "ghj",
      ]
    }
  },
  methods: {
    onItemsChanged (synonym) {
      // do something with array
    }
  }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM