Vue 双向数据绑定v-model
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script src="./lib/vue-2.4.0.js"></script> <style> h3{ color: rgb(75, 230, 14); font-weight: normal; font-size: 20px; } </style> </head> <body> <div id="app"> <input type="text" style="width:100%;" v-model="msg"> <h3>{{ msg }}</h3> <input type="text" style="width:100%;" v-model="msg1"> <h3>{{ msg1 }}</h3> <input type="button" value="点击改变data的数据" @click="add"> <!-- v-bind 只能实现数据的单向绑定,从 M 自动绑定到 V, 无法实现数据的双向绑定 --> <!-- <input type="text" v-bind:value="msg" style="width:100%;"> --> <!-- 使用 v-model 指令,可以实现 表单元素和 Model 中数据的双向数据绑定 --> <!-- 注意: v-model 只能运用在 表单元素中 --> <!-- input(radio, text, address, email....) select checkbox textarea --> </div> <script> // 创建 Vue 实例,得到 ViewModel var vm = new Vue({ el: '#app', data: { msg: '', msg1:'这里显示的是data里面的数据' }, methods: { //注意 这里的函数想取得data的数据 前面必须带上this add() { this.msg1='这里显示的已经修改过的数据' } } }); </script> </body> </html>