本文實(shí)例講述了vue實(shí)現(xiàn)的雙向數(shù)據(jù)綁定操作。分享給大家供大家參考,具體如下:
<!doctype html><html> <head> <meta charset="UTF-8"> <title>經(jīng)典的雙向數(shù)據(jù)綁定</title> <script src="https://cdn.bootcss.com/vue/2.0.1/vue.min.js"></script> </head> <body> <div id="container"> <button @click="modifyMsg">修改msg</button><!-- 方向1:將定義好的數(shù)據(jù)綁定到試圖 實(shí)現(xiàn)方式:雙花括號(hào) 常見(jiàn)指令:v-for v-if v-show …… --><!-- 方向2:將視圖中用戶操作的結(jié)果 綁定到指定的數(shù)據(jù) (多數(shù)指表單控件 input/textarea/select) 實(shí)現(xiàn)方式: v-model--> <p>{{msg}}</p> <input type="text" v-model="userAddress"> <p>{{"用戶修改的數(shù)據(jù):"+userAddress}}</p> <hr> <h2>v-model.number</h2> <input type="text" v-model.number="n"> <br> <input type="text" v-model.number="m"> <br> <button @click="getNum">求和</button> <span>{{num}}</span> <hr> <h2>v-model.trim</h2> <h5>沒(méi)有過(guò)濾首尾空格時(shí):</h5> <input type="text" v-model="myInput"> <span>{{myInput.length}}</span> <h5>過(guò)濾首尾空格時(shí):</h5> <input type="text" v-model.trim="myInput"> <span>{{myInput.length}}</span> <h2>v-moddel.lazy</h2> <h5>input失去焦點(diǎn)時(shí)才會(huì)輸出myMsg</h5> <input type="text" v-model.lazy="myMsg"> <span>{{myMsg}}</span> </div> <script> new Vue({ el:"#container", data:{ msg:"Hello VueJs", userAddress:"", n:0, m:0, num:0, myInput:0, myMsg:"" }, methods:{ modifyMsg:function(){ this.msg = "Hello Model" }, getNum:function(){ this.num = this.n+this.m; } } }) </script> </body></html>使用在線HTML/CSS/JavaScript代碼運(yùn)行工具:http://tools.VeVB.COm/code/HtmlJsRun測(cè)試,可得到如下運(yùn)行效果

感興趣的朋友可以測(cè)試一下看看運(yùn)行效果。
希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注