首页 > 其他分享 >Vue3之watch

Vue3之watch

时间:2022-12-27 13:56:46浏览次数:42  
标签:sum watch person oldValue Vue3 ref newValue

  watch监视ref函数

<template>
    <h2>当前求和为:{{sum}}</h2>
    <button @click="sum++">点我+1</button>
    <hr>
    <h2>当前的信息为:{{msg}}</h2>
    <button @click="msg+='!'">修改信息</button>
    <hr>
    <h2>姓名:{{person.name}}</h2>
    <h2>年龄:{{person.age}}</h2>
    <h2>薪资:{{person.job.j1.salary}}K</h2>
    <button @click="person.name+='~'">修改姓名</button>
    <button @click="person.age++">增长年龄</button>
    <button @click="person.job.j1.salary++">涨薪</button>
</template>

<script>
    import {ref,reactive,watch} from 'vue'
    export default {
        name: 'Demo',
        setup(){
            //数据
            let sum = ref(0)
            let msg = ref('你好啊')
            let person = ref({
                name:'张三',
                age:18,
                job:{
                    j1:{
                        salary:20
                    }
                }
            })

            console.log(person) 

           // 监视多个ref定义的响应式数据
            watch([sum,msg],(newValue,oldValue)=>{
                console.log('sum或msg变化了',newValue,oldValue)
            }) 
            watch(sum,(newValue,oldValue)=>{
                console.log('sum的值变化了',newValue,oldValue)
            })

            watch(person,(newValue,oldValue)=>{
                console.log('person的值变化了',newValue,oldValue)
            },{deep:true})


            //返回一个对象(常用)
            return {
                sum,
                msg,
                person
            }
        }
    }
</script>

 

 

 

 

 

 

 

 

 1

标签:sum,watch,person,oldValue,Vue3,ref,newValue
From: https://www.cnblogs.com/anjingdian/p/17007930.html

相关文章

  • Vue3之computed计算属性
    计算属性  computed函数与Vue2.x中computed配置功能一致写法<template><h1>一个人的信息</h1>姓:<inputtype="text"v-model="person.firstName">......
  • vue3_05使用reactive来处理复杂数据
    vue3中除了提供了ref函数以为还提供了reactive函数来操作数据,一般情况下我们使用ref函数来操作简单类型数据,reactive函数来操作复杂类型数据<template><div>{{objRet.na......
  • vue3_03ref操作复杂类型
    ref也可以将复杂类型的数据转换为响应式数据,使用方法和处理简单类型数据一样leta=ref(复杂类型数据)a.value.xxx<template><p>{{objref.num}}</p><button@c......
  • vue3_04ref获取标签
    ref也可以用来获取dom节点分为三步:1.给节点绑定ref='xxx'2.letxxx=ref()3.在挂载之后直接使用即可<template><divref="op">24</div><p>{{num}}</p>......
  • vue3中使用vuex
    一、使用习惯1(模块化):1、文件目录:2、userStore.tsimport{Module}from'vuex';//import{setStorage,getStorage}from"../../util/common";exportdefault{......
  • vue3_02ref操作简单类型
    vue3中提供了ref()函数可以把数据转换为响应式数据。<template><div>{{num}}</div><button@click="add">这是按钮</button></template><sc......
  • Vue3之setup的两个注意点
    setup的两个注意点setup执行的时机在beforeCreate之前执行一次,this是undefined。setup的参数props:值为对象,包含:组件外部传递过来,且组件内部声明接收了的属性。......
  • 关于Vue3 can not find module的报错
    最近在做Vue3项目的时候,会看到这样一个报错,找不到模块.vue文件,可能是ts文件无法识别vue后缀的文件导致的,上网搜索了一下发现尤大大给出了解决方案。在src目录下新建一个.......
  • Vue3之reactive和ref对比
    reactive对比ref从定义数据角度对比:ref用来定义:基本类型数据。reactive用来定义:对象(或数组)类型数据。备注:ref也可以用来定义对象(或数组)类型数据,它内部会自动通过re......
  • Vue3之ref
    ref函数作用:定义一个响应式的数据语法: constxxx=ref(initValue)创建一个包含响应式数据的引用对象(reference对象,简称ref对象)。JS中操作数据: xxx.value模板......