<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/vue-2.4.0.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="app"> <h4>{{msg}}</h4> <!-- 使用 v-model 指令,可以实现 表单元素和 Model 中数据的双向数据绑定 --> <!-- 注意: v-model 只能运用在 表单元素中 input(radio, text, address, email....) select checkbox textarea --> <input type="text" v-model="msg" /> <!-- v-bind只能单向给属性绑定变量,Mode层绑定View层 --> <input type="text" v-bind:value="msg" /> </div> <script type="text/javascript"> new Vue({ el:"#app", data:{ msg:"数据双向绑定!" } }) </script> </body> </html>
使用v-mode写的一个计算器
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/vue-2.4.0.js" type="text/javascript" charset="utf-8"></script> </head> <body> <div id="app"> <input type="text" v-model="n1"> <select v-model="opt"> <option value="+">+</option> <option value="-">-</option> <option value="*">*</option> <option value="/">/</option> </select> <input type="text" v-model="n2"> <input type="button" value="=" @click="calc"> <input type="text" v-model="result"> </div> <script> // 创建 Vue 实例,得到 ViewModel var vm = new Vue({ el: ‘#app‘, data: { n1: 0, n2: 0, result: 0, opt: ‘+‘ }, methods: { calc() { // 计算器算数的方法 // 逻辑: switch (this.opt) { case ‘+‘: this.result = parseInt(this.n1) + parseInt(this.n2) break; case ‘-‘: this.result = parseInt(this.n1) - parseInt(this.n2) break; case ‘*‘: this.result = parseInt(this.n1) * parseInt(this.n2) break; case ‘/‘: this.result = parseInt(this.n1) / parseInt(this.n2) break; } } } }); </script> </body> </html>
原文:https://www.cnblogs.com/shangrao/p/12827922.html