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

What is the difference between the box-size property content-box and border-box in the css box model?

 The box model is a very important concept in CSS layout, it includes content area, padding, border, and margin. Box models can be divided into two types: standard box models and IE box models. The box model, as the name suggests, is used to hold things, and the things it holds are the content of HTML elements. In other words, every visible HTML element is a box.

Js uses recursive way to traverse the dom tree to dynamically create element nodes

 What is a dom tree? In short, DOM is the Document Object Model, which provides a structured representation for documents and defines how to access the document structure through scripts. DOM is composed of nodes. After the HTML is loaded, the rendering engine will generate a DOM tree in memory based on the HTML document. This article uses a small case to traverse the dom tree recursively. The core of the method is to determine whether the incoming data is an array, and then traverse the root node. Note that there must be an end condition when using recursion.