Vue 侦听器

 

 

 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <div id="app">
        <h1>{{price}}</h1>
        <h1>{{memberPrice}}</h1>
        <h1>{{vipPrice}}</h1>
    </div>
    <script src="js/vue.3.2.2.js"></script>
    <script>
        // 1、创建Vue的实例对象
        const app = Vue.createApp({
            data(){//定义数据
                return {
                    msg:'你好!',
                    price:5,
                    vipPrice:0
                }
            },
            computed:{
                memberPrice(){
                    return this.price*0.8;
                }
            },
            //侦听器
            watch:{
                price(current,prev){
                    console.log(prev+' price change '+current);
                    //异步操作
                    setTimeout(()=>{
                        this.vipPrice=current*0.7;
                    },1000)
                }
            }
        }).mount('#app');
    </script>
</body>
</html>

 

posted @ 2021-08-24 16:04  残星  阅读(37)  评论(0编辑  收藏  举报