<!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="vue.js"></script> </head> <body> <div id="app"> <input type="text" v-model="firstName">+ <input type="text" v-model="lastName">= <input type="text" v-model="fullName"> </div> </body> <script> //创建一个vue实例 //当我们导入包之后,在浏览器的内存中就多了一个vue构造函数 var vm = new Vue({ el: '#app',//表示当前我们new的这个vue实例要控制页面上的哪个区域 data: { //data属性中存放的是el中要用到的数据 firstName: '', lastName: '', fullName: '', }, watch:{ //watch可以监听data中指定数据的变化,然后触发对应的处理函数 firstName:function(newVal,oldVal){ //console.log('firstName变化') //this.fullName = this.firstName+'_'+this.lastName this.fullName = newVal+'_'+this.lastName }, lastName:function(newVal,oldVal){ this.fullName = this.firstName+'_'+newVal } } }); </script> </html>