iis服务器助手广告广告
返回顶部
首页 > 资讯 > 前端开发 > 其他 >Vue3中的computed,watch,watchEffect如何使用
  • 678
分享到

Vue3中的computed,watch,watchEffect如何使用

Vue3watchcomputed 2023-05-14 23:05:44 678人浏览 独家记忆
摘要

一、computed<template> 姓:<input v-model="person.firstName"><br/><br/> 名:<input v

一、computed

<template>
  姓:<input v-model="person.firstName"><br/><br/>
  名:<input  v-model="person.lastName"><br/><br/>
  <span>全名:{{person.fullname}}</span><br/><br/>
  <span>全名:<input v-model="person.fullname"></span>
</template>

<script>
import {Reactive,computed} from 'Vue'
export default {
  name: 'HelloWorld',
  setup(){
    let person = reactive({
      firstName:"张",
      lastName:"三"
    })
    //computed简写形式,没考虑修改
    
    person.fullname = computed({
      get(){
        return person.firstName+"-"+person.lastName;
      },
      set(value){
        const nameArr = value.split('-');
        person.firstName = nameArr[0];
        person.lastName = nameArr[1];
      }
    })
    return{
      person,
    }
  }
}
</script>

二、watch

  • 1、与 Vue2.x 中 watch 配置功能一致

  • 2、两个小"坑":

    • 监视 reactive 定义的响应式数据时: oldValue 无法正确获取、强制开启了深度监视(deep配置失效)

    • 监视 reactive 定义的响应式数据中某个属性时:deep 配置有效

vu2 的写法

<template>
  <h3>当前求和为:{{ sum }}</h3>
  <button @click="sum++">点我sum++</button>
</template>

<script>
import {ref} from 'vue'

export default {
  name: 'Demo',
  watch: {
    
    sum: {
      immediate: true,
      deep:true,
      handler(newValue,oldValue) {
        console.log("sum发生了变化", newValue, oldValue);
      }
    }
  },
  setup() {
    let sum = ref(0);

    return {
      sum,
    }
  }
}
</script>

vue3 中这样写

1、情况一:监视ref所定义的一个响应式数据

<template>
  <h3>当前求和为:{{ sum }}</h3>
  <button @click="sum++">点我sum++</button>>
</template>
<script>
import {ref, watch} from 'vue'
export default {
  name: 'Demo',
  setup() {
    let sum = ref(0);
    let msg = ref("你好啊");
    //情况一:监视ref所定义的一个响应式数据
    watch(sum, (newValue, oldValue) => {
      console.log("sum发生了变化", newValue, oldValue);
    })

    return {
      sum
    }
  }
}
</script>

Vue3中的computed,watch,watchEffect如何使用

watch 还可以传一个配置项,把 immediate 等配置传进去:

watch(sum, (newValue, oldValue) => {
      console.log("sum发生了变化", newValue, oldValue);
    },{immediate:true})

2、情况二:当有多个信息需要同时监视时

<template>
  <h3>当前求和为:{{ sum }}</h3>
  <button @click="sum++">点我sum++</button>
  <hr/>
  <h3>信息为:{{ msg }}</h3>
  <button @click="msg+='!'">点我sum++</button>
</template>
<script>
import {ref, watch} from 'vue'
export default {
  name: 'Demo',
  setup() {
    let sum = ref(0);
    let msg = ref("你好啊");

    //情况二:监视ref所定义的多个响应式数据
    watch([sum,msg],(newValue, oldValue) => {
      console.log("sum发生了变化", newValue, oldValue);
    })

    return {
      sum,
      msg
    }
  }
}
</script>

3、情况三:监视reactive所定义的一个响应式数据

<template>
  <h3>姓名:{{ person.name }}</h3>
  <h3>年龄:{{ person.age }}</h3>
  <h3>薪资:{{ person.job.j1.salary }}K</h3>
  <button @click="person.name+='~'">修改姓名</button>
  <button @click="person.age++">修改年龄</button>
  <button @click="person.job.j1.salary++">涨薪</button>
</template>
<script>
import {reactive, watch} from 'vue'

export default {
  name: 'Demo',
  setup() {
    let person = reactive({
      name: "张三",
      age: 18,
      job:{
        j1:{
          salary:20
        }
      }
    })
    //情况三:监视reactive所定义的一个响应式数据全部属性
    // 1\注意:无法正确获取oldvalue
    // 2\注意:强制开启了深度监视(deep配置无效)
    watch(person, (newValue, oldValue) => {
      console.log("person发生了变化", newValue, oldValue);
    })

    return {
      person
    }
  }
}
</script>

4、情况四:监视reactive所定义的一个响应式数据某个属性

//情况四:监视reactive所定义的一个响应式数据某个属性
    watch(()=>person.name, (newValue, oldValue) => {
      console.log("person的name发生了变化", newValue, oldValue);
    })

Vue3中的computed,watch,watchEffect如何使用

5、情况五:监视 reactive 所定义的一个响应式数据某些属性

//情况五:监视reactive所定义的一个响应式数据某个属性
    watch([()=>person.name,()=>person.age], (newValue, oldValue) => {
      console.log("person的name或age发生了变化", newValue, oldValue);
    })

Vue3中的computed,watch,watchEffect如何使用

6、特殊情况,监视对象中的某个对象属性,要开始deep:true

watch(()=>person.job, (newValue, oldValue) => {
	console.log("person的job发生了变化", newValue, oldValue);
},{deep:true})//由于监视的是reactive对象中的某个属性,deep奏效

7、监视 ref 定义的对象响应数据,需要.value或deep:true

	let person = ref({
      name: "张三",
      age: 18,
      job:{
        j1:{
          salary:20
        }
      }
    })
    watch(person.value, (newValue, oldValue) => {
      console.log("person的value发生了变化", newValue, oldValue);
    })
    或
    watch(person, (newValue, oldValue) => {
      console.log("person的value发生了变化", newValue, oldValue);
    },{deep:true})

三、watchEffect

watch 的套路是:既要指明监视的属性,也要指明监视的回调

watchEffect 的套路是:不用指明监视哪个属性,监视的回调中用到哪个属性,那就监视哪个属性

watchEffect有点像computed

。但computed注重的计算出来的值(回调函数的返回值),所以必须要写返回值

。而watchEffect更注重的是过程(回调函数的函数体),所以不用写返回值

//watchEffect所指定的回调中用到的数据只要发生变化,则直接重新执行回调
watchEffect(()=>{
	const xl = sum.value
	const x2 = person.age
	console.log( "watchEffect配置的回调执行了")
})

例如还用上边的例子:

import {reactive,watchEffect} from 'vue'
export default {
  name: 'Demo',
  setup() {
    let person = reactive({
      name: "张三",
      age: 18,
      job:{
        j1:{
          salary:20
        }
      }
    })
    watchEffect(()=>{
      const x1 = person.name;
      console.log("watchEffect所指定的回调执行了"+x1);
    })
    return {
      person
    }
  }
}
</script>

最后,我们使用 watch 和 watchEffect 实现姓名的例子

<template>
  姓:<input v-model="person.firstName">
  名:<input  v-model="person.lastName">
  <span>全名:{{fullName}}</span>
  <span>全名:<input v-model="fullName"></span>
</template>
<script lang="ts">
import {defineComponent, reactive, ref,watch,watchEffect} from 'vue';
export default defineComponent({
  setup(){
    let person = reactive({
      firstName:"张",
      lastName:"三"
    });

    const fullName = ref('');

    watch(person,({firstName,lastName})=>{
      fullName.value = firstName+"-"+lastName
    },{immediate:true})

    //不用使用immediate,默认执行一次
    

    watchEffect(()=>{
      const name = fullName.value.split('-');
      person.firstName = name[0];
      person.lastName = name[1];
    })
    return{
      person,
      fullName
    }
  }
});
</script>
<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -WEBkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>

以上就是Vue3中的computed,watch,watchEffect如何使用的详细内容,更多请关注编程网其它相关文章!

--结束END--

本文标题: Vue3中的computed,watch,watchEffect如何使用

本文链接: https://www.lsjlt.com/news/208246.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Vue3中的computed,watch,watchEffect如何使用
    一、computed<template> 姓:<input v-model="person.firstName"><br/><br/> 名:<input v...
    99+
    2023-05-14
    Vue3 watch computed
  • Vue3中的computed,watch,watchEffect怎么使用
    这篇“Vue3中的computed,watch,watchEffect怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“...
    99+
    2023-07-06
  • Vue3中的 computed,watch,watchEffect的使用方法
    目录一、computed二、watchvu2 的写法Vue3 中这样写三、watchEffect一、computed <template> 姓:<input ...
    99+
    2024-04-02
  • vue3中的watch和watchEffect如何使用
    这篇文章主要介绍了vue3中的watch和watchEffect如何使用的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇vue3中的watch和watchEffect如何使用文章都会有所收获,下面我们一起来看看吧...
    99+
    2023-07-06
  • vue3中watch和watchEffect怎么使用
    这篇文章主要讲解了“vue3中watch和watchEffect怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“vue3中watch和watchEffect怎么使用”吧!一、watch...
    99+
    2023-06-21
  • vue3中的watch和computed怎么使用
    2.监听 ref 数据2.1监听一个 ref 数据<template> <p>{{ age }}</p> <button @click="age++">click&...
    99+
    2023-05-21
    Vue3 watch computed
  • vue3中的watch和watchEffect怎么用
    本篇内容介绍了“vue3中的watch和watchEffect怎么用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!首先总结一下两者的区别:w...
    99+
    2023-06-30
  • vue3中watch和watchEffect的新用法
    目录一、watch新用法1.1、watch使用语法1.2、watch监听多个属性值1.3、watch监听引用数据类型二、watchEffect三、watch与watchEffect区...
    99+
    2024-04-02
  • Vue3的Watch和computed怎么使用
    本篇内容介绍了“Vue3的Watch和computed怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
    99+
    2024-04-02
  • vue3中watch与watchEffect的区别
    目录vue3中watch与watchEffect的区别watch refwatch reactivewatchEffect对比扩展:vue3中的 watchEffect 和 watc...
    99+
    2023-02-17
    vue3 watch与watchEffect的区别 vue3 watch与watchEffect用法 vue3 watch与watchEffect
  • vue3中watch和watchEffect使用实例分析
    watchwatch监听单个数据<template> <input type="text" v-model="text1" /> </template> ...
    99+
    2023-05-14
    Vue3 watcheffect watch
  • vue3如何数据监听watch/watchEffect
    我们都知道监听器的作用是在每次响应式状态发生变化时触发,在组合式 API 中,我们可以使用 watch()函数和watchEffect()函数,当你更改了响应式状态,它可能会同时触发 Vue 组件更新和侦听器回调。默认情况下,用户创建的侦听...
    99+
    2023-05-14
    Vue3 watcheffect watch
  • VUE3中watch和watchEffect的用法详解
    watch和watchEffect都是监听器,但在写法和使用上有所区别。 watch在监听 ref 类型时和监听reactive类型时watch函数的写发有所不一样。watch在监听...
    99+
    2024-04-02
  • vue3怎么使用watch/watchEffect监听数据
    这篇文章主要介绍“vue3怎么使用watch/watchEffect监听数据”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“vue3怎么使用watch/watchEffect监听数据”文章能帮助大家解...
    99+
    2023-07-06
  • vue3中的watch和watchEffect实例详解
    目录首先总结一下两者的区别:下面是根据上面的第三点做的一些小实验:总结闲来无事,比较了一下 vue3 中的 watch 和 watchEffect,总觉得官方文档没咋说清楚,今天就小...
    99+
    2024-04-02
  • Vue3 中 watch 与 watchEffect 区别及用法小结
    目录响应式依赖收集WatchWatchEffect什么时候用什么?大部分时候用 watch 显式的指定依赖以避免不必要的重复触发,也避免在后续代码修改或重构时不小心引入新的依赖。wa...
    99+
    2024-04-02
  • Vue3中的setup语法糖、computed函数、watch函数如何用
    computed函数computed 函数的使用:其实我们什么情况下会使用计算属性呢,那一定是通过依赖的数据得到新的数据!1)从Vue中引入computed 2)在setup中进行使用,将一个函数,函数的返回值就是计算好的数据 3)最后呢通...
    99+
    2023-05-17
    Vue3 setup computed
  • Vue3中watchEffect侦听器如何使用
    本篇内容介绍了“Vue3中watchEffect侦听器如何使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!watchEffect 侦听器其...
    99+
    2023-07-02
  • Vue3中的setup语法糖、computed函数、watch函数怎么使用
    这篇文章主要介绍“Vue3中的setup语法糖、computed函数、watch函数怎么使用”,在日常操作中,相信很多人在Vue3中的setup语法糖、computed函数、watch函数怎么使用问题上存在疑惑,小编查阅了各式资料,整理出简...
    99+
    2023-07-05
  • 详解Vue3中的watch侦听器和watchEffect高级侦听器
    目录1watch侦听器2watchEffect高级侦听器清除副作用:就是在触发监听之前会调用一个函数可以处理你的逻辑例如防抖停止跟踪 watchEffect 返回一个函数 调用之后将...
    99+
    2022-11-13
    Vue3 watch侦听器 vue3 watchEffect侦听器
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作