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

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.

Use of custom events in Vue components.

 The parent component uses props to pass data (such as a method) to the child component, and the child component needs to use custom events to pass data back to the parent component! We can use v-on to bind custom events, and then pass them to subcomponents, which will call after receiving the method. Each Vue instance implements the Events interface, that is: use $on(eventName) to listen for events and $emit(eventName) to trigger events.

mapState, mapGetters, mapMutations, mapActions in vuex

 The above four objects are all in the vuex plug-in store, which is often used to store some data shared globally. But I still feel a little uncomfortable when using it, that is, I wrote a lot of repeated prefixes. At this time, these four objects appeared, which solved this pain point very well. mapState is mainly used to manipulate and obtain data, mapGetters is equivalent to computed properties, and mapMutations and mapActions are simplifications of mutation and action. They can be written as both objects and arrays.