vue中watch记录变量变化的值
vue中watch记录变量变化的值
<!DOCTYPE html> <html lang="en"> <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="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script> </head> <body> <div id="app">{{a}}</div> <script> var data = {a:1} var vm = new Vue({ el: "#app", data:data }) //$watch是一个实例方法,可以观察变量的变化,放在变量变化之前,会记录变化后最新的值和之前的值 vm.$watch('a',function(newValue,oldValue){ //这个回调将在'vm.a'改变后调用 console.log(newValue,oldValue) //2 1 }) vm.$data.a='2' </script> </body> </html>