Skip to main content

How to understand the v-model directive in Vue?

 What is the v-model directive? In short, it is Vue's two-way binding instruction, which can synchronously update the value input by the control on the page to the data attribute of the relevant binding, and also update the template value on the page when the original data data is updated. It mainly provides two functions. The input value of the view layer affects the attribute value of the data, and the change of the data attribute value will update the value change of the view layer.

The following is a direct case demonstration for easy understanding.

<div id="app">
        <form>
            name:<input type="text" v-model.trim="obj.name"><br>
            age:<input type="number" v-model.number="obj.age"><br>
  sex:<label>man<input type="radio" name="sex" value="man" v-model="obj.sex"></label>
<label>woman<input type="radio" name="sex" value="woman" v-model="obj.sex"></label><br>
            hobby:sing<input type="checkbox" v-model="obj.hobby" value="sing">
            jump<input type="checkbox" v-model="obj.hobby" value="jump">
            rap<input type="checkbox" v-model="obj.hobby" value="rap">
         basketball<input type="checkbox" v-model="obj.hobby" value="basketball"><br>
            address:<select v-model="obj.addr">
                <option value="">please choose</option>
                <option value="aaa">aaa</option>
                <option value="bbb">bbb</option>
                <option value="ccc">ccc</option>
                <option value="ddd">ddd</option>
            </select><br>
            other:<textarea v-model.lazy="obj.others"></textarea><br>
            agree:<input type="checkbox" v-model="obj.agree"><br>
            <button v-on:click.prevent="send()">send</button>
       
        </form>
    </div>

    <script>
     Vue.config.productionTip=false
        var vm = new Vue({
            el: '#app',
            data: {
                obj:{
                    name:"",
                    age:"",
                    sex:"man",
                    hobby:[],
                    others:"",
                    agree:""
                }
            },
            methods: {
                send(){
                    console.log(JSON.stringify(this.obj));
//{"name":"aaa","age":100,"sex":"man","hobby":["jump","rap","basket ....}                   
                }
            }
        });
    </script>
Important: When multiple checkboxes are checked, the value of value will also be automatically pushed into this array. v-model can also add modifiers to control the timing of data synchronization. For example.number,.trim,.lazy......

Comments

Popular posts from this blog

A simple understanding of ES6 iterators

 What is an iterator?An iterator is an interface that provides a unified access mechanism for various data structures. Any data structure can complete the traversal operation as long as the iterator interface is deployed.ES6 created a new traversal command for...of loop, which natively has a data structure with the iterator interface (which can be traversed with for...of). Contains Array, Arguments, Set, Map, String, TypedArray, NodeList. Of course, you can also implement this interface manually, which is convenient for practical use.