再看那柔弱的柳树吧,在寒冬余威尚盛时节,就早早苏醒过来,望着冰冻的河面,迎着凛冽的寒风,它微微察觉出一丝春意,于是,不顾一切地率先吐翠,淡淡地披起娇黄嫩绿的新装。沿河望去,枝梢间烟纱雾彀,一片生机,这情景仿佛一首动人的歌,一首热烈向往春天的歌,一首报告春的信息的歌,一首表达美好信念的歌。我在想:既然迎春花被人称作报春花,那么,柳树可不可以叫作报春树呢春来了,万千柳枝在春风中袅袅舞动。柳树是热爱春天的,春天也是热爱柳树的。
本文主要讲解v-model指令,主要包括HTML元素的v-model和组件上的v-model两种,用四个简单的案例介绍v-model的使用。
案例一:HTML元素的v-model -输入框(text)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-model指令</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app4"> {{price}}<br> <input v-model="price"><!-- 下行注释的语法糖 --> <!-- <input :value="price" @input="price = $event.target.value"> --> </div> <script> new Vue({ el: '#app4', data: { price: '20' } }); </script> </body> </html>
案例二: 定制组件的v-model - 输入框(text)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-model指令</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app3"> {{price}} <my-input v-model="price"></my-input><!-- 下行注释的语法糖 --> <!-- <my-input :value="price" @input="val => {price = val}"></my-input> --> </div> <script> Vue.component('my-input', { template: '<div></span><input type="text" ref="input" :value="value" @input="doThis"/></div>', props: { value: String }, methods: { doThis() { this.$emit('input', this.$refs.input.value); } } }); new Vue({ el: '#app3', data: { price: '10' } }); </script> </body> </html>
案例三: 定制组件的v-model - 复选框(checkbox) - 2.2.0 新增
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-model指令</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app2"> {{fruit}}<!-- 观测数据变化 --> <my-checkbox v-model="fruit.apple" value="apple"></my-checkbox><!-- 下行注释的语法糖 --> <!-- <my-checkbox :checked="fruit.apple" @change="val => {fruit.apple = val}" value="apple"></my-checkbox> --> <my-checkbox v-model="fruit.peach" value="peach"></my-checkbox><!-- 下行注释的语法糖 --> <!-- <my-checkbox :checked="fruit.peach" @change="val => {fruit.peach = val}" value="peach"></my-checkbox> --> </div> <script> Vue.component('my-checkbox', { template: '<div><span>{{value}}</span><input type="checkbox" :checked="checked" @change="doThis" :value="value"/></div>', model: { prop: 'checked', event: 'change' }, props: { checked: Boolean, value: String }, methods: { doThis() { this.$emit('change', !this.checked); } } }); new Vue({ el: '#app2', data: { fruit: {//数据 apple: true, peach: false } } }); </script> </body> </html>
案例四: 定制组件的v-model - 单选按钮(radio) -2.2.0 新增
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>v-model指令</title> <script src="https://cdn.bootcss.com/vue/2.3.4/vue.js"></script> </head> <body> <div id="app"> {{fruit}}<!-- 观测数据变化 --> <my-radio v-model="fruit" value="apple" name="myFruit"></my-radio><!-- 下行注释的语法糖 --> <!-- <my-radio :checked="fruit" @change="val => {fruit = val}" value="apple"></my-radio> --> <my-radio v-model="fruit" value="peach" name="myFruit"></my-radio><!-- 下行注释的语法糖 --> <!-- <my-radio :checked="fruit" @change="val => {fruit = val}" value="peach"></my-radio> --> </div> <script> Vue.component('my-radio', { template: '<div><span>{{value}}</span><input :name="name" type="radio" ref="radio" :checked="checked===value" @change="doThis" :value="value"/></div>', model: { prop: 'checked', event: 'change' }, props: { checked: String, value: String, name: String }, methods: { doThis() { this.$emit('change', this.$refs.radio.value); } } }); new Vue({ el: '#app', data: { fruit: 'peach'//数据 } }); </script> </body> </html>
以上就是如何理解Vue的v-model指令的如何使用方法。但行前路,不负韶华!更多关于如何理解Vue的v-model指令的如何使用方法请关注haodaima.com其它相关文章!