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 width property 100% and auto in css?

 width:auto: The default width value of block-level elements. When set to this value, the browser will automatically select an appropriate width value to adapt to the width of the parent element. When the width is set to 100%, the width of the child element box The value is equal to the parent's content, and as the parent's content automatically changes, after adding the padding and margin of the child element, its width remains unchanged, which is the difference from setting it to auto. But we most often use width:auto, because it is more flexible, width:100% is used less, because when adding padding or margin, this method is easy to make it exceed the parent box and destroy the original layout.

Access to XMLHttpRequest at 'http://localhost:8080/xxx' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

 Due to browser same-origin policy restrictions. The Same Origin Policy is a convention. This is the main and essential security feature of the browser. Without the same-origin policy, normal browser functionality may be affected. The web is built on the same-origin policy, and browsers are just one implementation of the same-origin policy. The Same Origin Policy prevents JavaScript scripts from one domain from interacting with content from another domain. The so-called same origin (that is, the same domain) means that two pages have the same protocol, host and port.