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 >
<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>
以上是 Vue的watch监听事件 的全部内容, 来源链接: utcz.com/z/377276.html