Vue的watch监听事件

时间:2023-03-09 16:18:14
Vue的watch监听事件

Vue的watch监听事件

相关Html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>名称案例</title>
<script src="../js/vue-2.4.0.js"></script>
</head>
<body>
<div id="app"> <input type="text" v-model="firstname" @keyup="getFullname">+
<input type="text" v-model="lastname" @keyup="getFullname">=
<input type="text" v-model="fullname"> </div> <script> var vm = new Vue({ el: "#app",
data: {
firstname: "",
lastname: "",
fullname: ""
},
methods: {
getFullname: function () {
// this.fullname = this.firstname+this.lastname;
}
},
watch: {
//使用这个可以监听data中指定数据的变化,然后触发watch中对应的function的处理
'firstname': function (newVal,oldVal) { console.log((newVal + "--" + oldVal));
},
'lastname': function (newVal,oldVal) {
console.log((newVal + "--" + oldVal));
} } }) </script> </body>
</html>