Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"You are binding v-model directly to a v-for iteration alias"

Tags:

vue.js

Just run into this error I hadn't encountered before: "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 of objects and use v-model on an object property instead." I am a little puzzled, as I don't appear to be doing anythong wrong. The only difference from other v-for loops I've used before is that this one is a little simpler, in that it's simply looping through an array of strings, rather than objects:

 <tr v-for="(run, index) in this.settings.runs">       <td>          <text-field :name="'run'+index" v-model="run"/>      </td>       <td>         <button @click.prevent="removeRun(index)">X</button>      </td>   </tr> 

The error message would seem to suggest that I need to actually make things a little more complicated, and use objects instead of simple strings, which somehow doesn't seem right to me. Am I missing something?

like image 445
John Moore Avatar asked Mar 06 '17 15:03

John Moore


People also ask

What is V-bind vs V-model?

v-bind is a one-way binding. v-model is used for binding form elements like inputs, radio buttons, textarea, checkboxes. It is used for binding data, attributes, expressions, class, styles. V-model is input value sensitive.

Is V-model two-way binding?

By design, the v-model directive allows us to bind an input value to the state of an app. We use it to create a two-way data binding on the form input, textarea, and select elements.

What does V-bind do in Vue?

In Vue, v-bind lets you bind an HTML attribute to a JavaScript expression. There are two broad use cases for this one-way data binding: Binding to built-in attributes, like href or class. Passing props to a child component.

What is V-bind value?

v-bind is used to bind html attribute But v-bind:value is called one way binding that means: you can change input value by changing bound data but you can't change bound data by changing input value through the element. v-model is intended to be used with form elements.


1 Answers

Since you're using v-model, you expect to be able to update the run value from the input field (text-field is a component based on text input field, I assume).

The message is telling you that you can't directly modify a v-for alias (which run is). Instead you can use index to refer to the desired element. You would similarly use index in removeRun.

new Vue({    el: '#app',    data: {      settings: {        runs: [1, 2, 3]      }    },    methods: {      removeRun: function(i) {        console.log("Remove", i);        this.settings.runs.splice(i,1);      }    }  });
<script src="//cdnjs.cloudflare.com/ajax/libs/vue/2.5.18/vue.js"></script>  <table id="app">    <tr v-for="(run, index) in settings.runs">      <td>        <input type="text" :name="'run'+index" v-model="settings.runs[index]" />      </td>      <td>        <button @click.prevent="removeRun(index)">X</button>      </td>      <td>{{run}}</td>    </tr>  </table>
like image 149
Roy J Avatar answered Sep 18 '22 15:09

Roy J