$watch使用

<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="Vue.js"></script>
</head>
<body>
<div id="app">
<input v-model="title"></input>
<h1>{{title}}</h1>
<button @click="startListen">开始监听</button>
<button @click="stopListen">停止监听</button>
</div>
</body>
</html>
<script>
var app = new Vue({
el: '#app',
data: {
title: 'hello',
isListen: false,
unWatch:null
},
methods: {
startListen() {
this.unWatch = this.$watch('title', (oldVal, newVal) => {
console.log(oldVal, newVal)
});
},
stopListen(){
this.unWatch();
}
}
});
</script>