vue属性监听watch,监听对象中属性的变化
watch和data、methods等同级
watch两个参数(newValue,odlValue) 分别为新数据和旧数据
<!DOCTYPE html> <html> <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></title> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script src="https://unpkg.com/vue-router/dist/vue-router.js"></script> <script src="https://unpkg.com/axios/dist/axios.min.js"></script> </head> <body> <div id='app'> <input type="text" v-model='msg'> </div> <script> const vm = new Vue({ el: '#app', data: { msg:'111' }, methods: { }, // 属性监听 watch:{ // 两个参数(新数据,旧数据) msg(newValue,oldvalue){ // 监听input框变化,分别打印新数据和旧数据 console.log(newValue); console.log(oldvalue); } } }) </script> </body> </html>
监听对象中单个属性的变化
watch:{ "drawer.drawer": { handler(newValue, oldValue) { console.log("单个属性监听", newValue, oldValue); } },