Why v-model doesn't work with an array and v-for loop?

前端 未结 2 957
南方客
南方客 2021-02-14 12:05

I got a custom select component, it works with a simple variable, but when used with v-for it won\'t work:

https://jsfiddle.net/7gjkbhy3/19/



        
2条回答
  •  盖世英雄少女心
    2021-02-14 12:24

    v-model and v-for do NOT go together well if v-model is used to an iteration alias w/ a primitive value.

    The Vue warns:

    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.

    Therefore using an array of objects each of which has a property for the select value would solve the issue:

    WORKING EXAMPLE.

    
    
    new Vue({
         el: '#app',
         data: {
             sample: 0,
             samples : [{ value: 0 }, { value: 0 }, { value: 0 }]
         }
     })
    

提交回复
热议问题