2020-06-05——watch vue事件監(jiān)聽

// 監(jiān)聽 類似于數(shù)據(jù)庫中的觸發(fā)器
watch:{
// name值修改后觸發(fā)
name:function(newValue,oldValue){
console.log("修改之前的數(shù)據(jù):"+oldValue+"修改之后的數(shù)據(jù):"+newValue);
},
// 對象屬性的監(jiān)聽
// "user.id":function(newValue,oldValue){
// console.log("修改之前的數(shù)據(jù):"+oldValue+"修改之后的數(shù)據(jù):"+newValue);
// },
// 對象的監(jiān)聽
user:{
handler:function(newValue,oldValue){
console.log("修改之前的數(shù)據(jù):"+oldValue.id+"修改之后的數(shù)據(jù):"+newValue.id);
},
// 深度監(jiān)聽 當對象中的屬性發(fā)生改變后也會監(jiān)聽
deep:true
}
}

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <title>watch vue事件監(jiān)聽</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,
                        
                    }
                },
                // 監(jiān)聽 類似于數(shù)據(jù)庫中的觸發(fā)器
                watch:{
                    // name值修改后觸發(fā)
                    name:function(newValue,oldValue){
                        console.log("修改之前的數(shù)據(jù):"+oldValue+"修改之后的數(shù)據(jù):"+newValue);
                    },
                    // 對象屬性的監(jiān)聽
                    // "user.id":function(newValue,oldValue){
                    //  console.log("修改之前的數(shù)據(jù):"+oldValue+"修改之后的數(shù)據(jù):"+newValue);
                    // },
                    // 對象的監(jiān)聽
                    user:{
                        handler:function(newValue,oldValue){
                                console.log("修改之前的數(shù)據(jù):"+oldValue.id+"修改之后的數(shù)據(jù):"+newValue.id);
                        },
                        // 深度監(jiān)聽 當對象中的屬性發(fā)生改變后也會監(jiān)聽
                        deep:true 
                        
                        
                    }
                }
            });
            
            // 手動銷毀
            // vm.$destroy();
            
            vm.message="tom";
            
            // 獲取元素的內(nèi)容 打印
            console.log(vm.$refs.title.textContent);
            // 打印內(nèi)容不一樣的原因
            // 獲取內(nèi)容的時候,組件內(nèi)容dom元素還沒有更新完畢
            
            // 組件更新完之后再顯示
            vm.$nextTick(function(){
                console.log(vm.$refs.title.textContent);
            })
            
            
            
        </script>
    </body>
</html>

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容