vue学习笔记(19)——监视的简写形式

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>天气案例_监视属性_简写</title>
    <!-- 引入Vue -->
    <script type="text/javascript" src="../js/vue.js"></script>
  
</head>
<body>
<!-- 准备一个容器 -->
<div id="root">
 <h2>今天天气很{
    
    {info}}</h2>
 <button @click="changeweather">切换天气</button>
</div>
 
<script type="text/javascript">
Vue.config.productionTip=false


const vm=new Vue({
    el:'#root',
    data:{
        ishot:true,
        
    },
    computed:{
        info(){
            return this.ishot?'hot':'cool'
        }
    },
    methods:{
        changeweather(){
            this.ishot=!this.ishot
        },

    },

    watch:{
        //正常写法
       /* // 计算属性也可以被监视
        ishot:{
            // immediate:true,//初始化时让handler调用一下
            // deep:true,//深度监视
             // handler当ishot发生改变时调用
            handler(newValue,oldValue){
                console.log('ishot被调用了',newValue,oldValue)
            }
        },*/

        //简写
        // ishot(newValue,oldValue){
        //     console.log('ishot被调用了',newValue,oldValue)
        // }

    }
})
//正常写法
// vm.$watch('ishot',{
//             immediate:true,//初始化时让handler调用一下
//             deep:true,//深度监视
//              // handler当ishot发生改变时调用
//              handler(newValue,oldValue){
//                 console.log('ishot被调用了',newValue,oldValue)
//             }
// })

 //简写
vm.$watch('ishot',function(newValue,oldValue){
    console.log('ishot被调用了',newValue,oldValue)
})
</script>
</body>
</html>

猜你喜欢

转载自blog.csdn.net/weixin_54763080/article/details/128840632