<!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>