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

Destructuring assignment in ES6

 In the past, when we wanted to assign a value to a variable, such as an array type and an object type, to assign a value to a variable, we could only specify the value directly. It is very troublesome to write in this way, but under the ES6 syntax specification, it is allowed to directly extract the required values from arrays and objects according to a certain pattern, and directly assign variables to variables. This method is called Destructuring, which is simple to understand. That is, the left and right sides of the equal sign are equal.

favicon.ico 404 Not Found Causes and Solutions

 What is favicon? It is the abbreviation of Favorites Icon. Its function is that in addition to displaying the corresponding title in the browser's favorites, icons can also be used to distinguish different websites. When I encounter this kind of error, I feel very uncomfortable. There is obviously no problem. Why is a 404 error reported? The following is the solution.