image.png

代码

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vue.js 监听属性-普通的 watch 实现计数器</title>
    <script src="//static.gzj2001.com/vue/vue.js"></script>
</head>
<body>
    <div id="app">
        <p>计数器:{{jishuqi}}</p>
        <div>改变之前:{{ov}}</div>
        <div>改变之后:{{nv}}</div>
        <button @click="jishuqi++">点击</button>
    </div>
    <script>
      let vm =  new Vue({
            el:"#app",
            data:{
                jishuqi:1,
                ov:0,
                nv:0
            }
        })

        vm.$watch("jishuqi",function(nval,oval){
            vm.$data.ov=oval;
            vm.$data.nv=nval;
            
        })
    </script>
</body>
</html>