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

后端 未结 1 682
别那么骄傲
别那么骄傲 2020-12-08 13:34

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

相关标签:
1条回答
  • 2020-12-08 14:00

    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>

    0 讨论(0)
提交回复
热议问题