Vue----watch监测数据变化

一.为什么要用 watch
当某些数据一旦发生变化,我们就想要采取某种措施时使用
示例

当 price 一旦发生变化时,我们想要计算出 priceDifferences
<template>
    <div class="watch">
        <h2>{{price}}</h2>
        <h2>{{priceDifferences}}</h2>
        <button @click="addprice">price *2</button>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                priceDifferences:0,
                price:100
            }
        },
        watch:{
            price:function(newVal,oldValue){
                this.priceDifferences = newVal - oldValue
                console.log(this.priceDifferences)
            }
        },
        methods:{
            addprice(){
                this.price =  this.price *2 
            }
        }
    }
</script>

二.watch的用法

1.普通监听(data 里的普通数据)

<template>
    <div class="watch">
        <div>{{msg}}</div>
        <button @click="changemsg">changemsg</button>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                msg:'这是一个普通监听'
            }
        },
        watch:{
            msg:function(newval,oldval){
                console.log('由',oldval,'到',newval)
            }
        },
        methods:{
            changemsg(){
                this.msg = "嘿嘿嘿,是不是监听到了呢"
            }
        }
    }
</script>

运行结果
点击前
在这里插入图片描述
点击后
在这里插入图片描述
控制台输出
在这里插入图片描述
2.监听数组

<template>
    <div class="watch">
        <div v-for="(a,aindex) in arr" :key="aindex">{{a}}</div>
        <button @click="changearr">changearr</button>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                arr:[1,2,3]
            }
        },
        watch:{
            arr:{
                handler:function(newVal){
                    console.log('arr newVal',newVal)
                }
            }
        },
        methods:{
            changearr(){
                this.arr = this.arr.reverse();
            }
        }
    }
</script>

运行结果
点击前
在这里插入图片描述
点击后
在这里插入图片描述
控制台输出
在这里插入图片描述
3.监听对象
监听对象时有两种方法:

1)
obj:{
   handler:function(newVal){
   console.log('newVal',newVal)
  },
   deep:true
}
2)
'obj.course':function(newval,oldval){
	console.log("这是第二种监测方法",newval)
 }
完整的代码
<template>
    <div class="watch">
        <h1>{{obj.name}}</h1>
        <h2>{{obj.age}}</h2>
        <div>{{obj.sex}}</div>
        <h3 v-for="(cou,couindex) in obj.course" :key="couindex">{{cou}}</h3>
        <button @click="addAge">age ++</button>
        <button @click="changeCourse">change course</button>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                obj:{
                    course:{
                        one:'chinese',
                        two:'english',
                        three:'math'
                    }
                }
            }
        },
        watch:{
        //第一种方法
            obj:{
                handler:function(newVal){
                    console.log('newVal',newVal)
                },
                deep:true
            },
            //第二种方法
            'obj.course':function(newval,oldval){
                console.log("这是第二种监测方法",newval)
            }
        },
        methods:{
        		changeCourse(){
                this.obj.course = {
                    one:'语文',
                    two:'英语',
                    three:'数学'
                }
            }
        }
    }
</script>

运行结果
点击前
在这里插入图片描述
点击后
在这里插入图片描述
控制台输出
在这里插入图片描述
注意:当监听对象时 增加属性时监测不到,因为该属性没有在 data 里注册,不是响应式的
解决办法:Vue.set(object, key, value)或者vm.$set方法(全局 Vue.set 方法的别名)

<template>
    <div class="watch">
        <h1>{{obj.name}}</h1>
        <h2>{{obj.age}}</h2>
        <div>{{obj.sex}}</div>
        <h3 v-for="(cou,couindex) in obj.course" :key="couindex">{{cou}}</h3>
        <button @click="addsex">addsex</button>
    </div>
</template>
<script>
    export default{
        data(){
            return{
                obj:{
                    name:'lily',
                    age:'23',
                    course:{
                        one:'chinese',
                        two:'english',
                        three:'math'
                    }
                }
            }
        },
        watch:{
            obj:{
                handler:function(newVal){
                    console.log('newVal',newVal)
                },
                deep:true
            }
        },
        methods:{
            addsex(){
            //监测不到,控制台不会打印改变后的值
            // this.obj.sex = "female" 
            //使用 $set 之后可以监测到值
                this.$set(this.obj,'sex','female')
            }
        }
    }
</script>

运行结果
点击前
在这里插入图片描述
点击后
在这里插入图片描述
控制台输出
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue中,我们可以使用watch选项来监测数据变化监测的对象可以是data中已存在的、函数、methods中的函数名,以及computed中的。当监测数据发生变化,相应的方法会被触发执行。具体的写法有以下几种: 1. 监测data中已存在的: ```javascript watch: { a: function(newVal, oldVal) { // 执行相关操作 console.log(newVal, oldVal); } } ``` 2. 监测函数: ```javascript watch: { data() { console.log('数据发生变化'); } } ``` 3. 监测methods中的函数名: ```javascript watch: { data: 'changeData' // 为methods中的方法名 }, methods: { changeData(curVal, oldVal) { console.log(curVal, oldVal); } } ``` 4. 监测computed中的: ```javascript watch: { computedValue: function(newVal, oldVal) { console.log(newVal, oldVal); } } ``` 需要注意的是,watch监听的对象也可以是包括选项的对象,具体写法根据需求进行调整。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [vue watch关于对象内的属性监听](https://download.csdn.net/download/weixin_38555616/13667758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [vuewatch使用 监听数据变化](https://blog.csdn.net/zsq199771/article/details/125481011)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值