2020-06-05——watch vue事件监听

// 监听 类似于数据库中的触发器
watch:{
// name值修改后触发
name:function(newValue,oldValue){
console.log("修改之前的数据:"+oldValue+"修改之后的数据:"+newValue);
},
// 对象属性的监听
// "user.id":function(newValue,oldValue){
// console.log("修改之前的数据:"+oldValue+"修改之后的数据:"+newValue);
// },
// 对象的监听
user:{
handler:function(newValue,oldValue){
console.log("修改之前的数据:"+oldValue.id+"修改之后的数据:"+newValue.id);
},
// 深度监听 当对象中的属性发生改变后也会监听
deep:true
}
}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>watch vue事件监听</title>
        <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
        <script src="js/vue.js" type="text/javascript" charset="utf-8"></script>
    </head>
    <body>
        <div id="app">
            <input type="text" v-model="name"/>
            <input type="text" v-model="user.id"/>
        </div>
        <script type="text/javascript">
            // 挂载的两种方法
            // var vm = new Vue({
            //  data:{
                    
            //  }
            // }).$mount("#app");
            
            var vm = new Vue({
                el:"#app",
                data:{
                    name:"aaa",
                    user:{
                        id:001,
                        
                    }
                },
                // 监听 类似于数据库中的触发器
                watch:{
                    // name值修改后触发
                    name:function(newValue,oldValue){
                        console.log("修改之前的数据:"+oldValue+"修改之后的数据:"+newValue);
                    },
                    // 对象属性的监听
                    // "user.id":function(newValue,oldValue){
                    //  console.log("修改之前的数据:"+oldValue+"修改之后的数据:"+newValue);
                    // },
                    // 对象的监听
                    user:{
                        handler:function(newValue,oldValue){
                                console.log("修改之前的数据:"+oldValue.id+"修改之后的数据:"+newValue.id);
                        },
                        // 深度监听 当对象中的属性发生改变后也会监听
                        deep:true 
                        
                        
                    }
                }
            });
            
            // 手动销毁
            // vm.$destroy();
            
            vm.message="tom";
            
            // 获取元素的内容 打印
            console.log(vm.$refs.title.textContent);
            // 打印内容不一样的原因
            // 获取内容的时候,组件内容dom元素还没有更新完毕
            
            // 组件更新完之后再显示
            vm.$nextTick(function(){
                console.log(vm.$refs.title.textContent);
            })
            
            
            
        </script>
    </body>
</html>

案例地址:http://jingrao.tianyuyezi.com/vue3/demo6.html

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容