作用:监听数据(一旦数据符合判断,则执行自定义操作)
<!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>侦听器watch</title></head><body><div id='app'><input type="text" v-model='msg'><h3>{{msg}}</h3><h3>{{stus[0].name}}</h3><button @click='stus[0].name = "Tom"'>改变</button></div><script src="./vue.js"></script><script>// 基本的数据类型可以使用watch直接监听,复杂数据类型Object Array 要深度监视new Vue({el: '#app',data: {msg:'',stus:[{name:'jack'}]},watch: {// key是属于data对象的属性名 value:监视后的行为 newV :新值 oldV:旧值'msg':function(newV,oldV){// console.log(newV,oldV);if(newV === '100'){console.log('hello');}},// 深度监视: Object |Array"stus":{deep:'true',handler:function(newV,oldV){console.log(newV[0].name);}}},})</script></body></html>
