(エッセンス2020年5月5日更新)vueチュートリアル編データ傍受watchの使用


<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>       :$watch</title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
</head>
<body>
        <div id="app">
            <input type="text" v-model="name">
            <h3>{{name}}</h3>
            <hr>
            
            <input type="text" v-model="age">
            <h3>{{age}}</h3>
            <hr>
    
            <input type="text" v-model="user.name">
            <h3>{{user.name}}</h3>
    
            <!-- <input type="text" v-model="user.links.link">
            <h3>{{user.links.link}}</h3> -->
        </div>

    <script>
        var vm = new Vue({
            el:'#app',
            data(){
                return {
                    name:'tom',
                    age:20,
                    user:{
                        name:'song',
                        age:10
                    }
                }
            },
            //vue   watch  ,         
            watch:{
                age:function(newVal,oldVal){
                    console.log('age    ,    '+oldVal+'  :' + newVal);
                },
                // age:(newVal,oldVal)=>{ },
                // age(newVal,oldVal){ }
                // 'user.name':function(newVal,oldVal){
                //     console.log('user.name    ,    '+oldVal+'  :' + newVal);
                // },
                user:{
                    handler(newVal,oldVal){
                        debugger
                        console.log('newVal',newVal);
                        console.log('oldVal',oldVal);
                    },
                    deep:true //    ,                 
                }
            },
            methods:{},
            created(){},
            beforeMount(){},
            mounted(){}
        });
        //vue     $watch  
        vm.$watch('name',function(newVal,oldVal){
            console.log('name    ,    '+oldVal+'  :' + newVal);
        })
    </script>
</body>
</html>