继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Vue中的$watch监控数据

android开发学习视频
关注TA
已关注
手记 304
粉丝 52
获赞 322

         

<!DOCTYPE html><html>    <head>        <meta charset="utf-8" />        <title>监控数据的变化</title>    </head>    <script type="text/javascript" src="js/vue.js" ></script>    <body>        <div id="div1">            <input type="text" v-model="name">            <h2>{{name}}</h2>            <hr>            <input type="text" v-model="age">            <h2>{{age}}</h2>            <input type="text" v-model="user.age">            <h2>{{user.age}}</h2>        </div>    </body>    <script>        let vm = new Vue({            el: "#div1",            data:{                name:'Tom',                age:18,                user:{                    id:1,                    age:20                }            },            watch:{                //方式一:监控vue实例的数据                age:(newValue,oldValue) => {                    console.log('name的newValue值为:'+newValue+',旧值为:'+oldValue);                },                user:{                    handler:(newValue,oldValue) => {                        console.log('age的newValue值为:'+newValue.age+',旧值为:'+oldValue.age);                        //原来的旧值已经看不见了,只能看到新的值                    },                    deep: true //深度监视,当对象中的属性发生变化时会被监控                }            }        });        //方式二:监控vue实例的数据        vm.$watch('name',function(newValue,oldValue){                console.log('name的newValue值为:'+newValue+',旧值为:'+oldValue);            });    </script></html>


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP