广告
返回顶部
首页 > 资讯 > 精选 >Vue中的Computed与watch怎么用
  • 948
分享到

Vue中的Computed与watch怎么用

2023-06-30 10:06:45 948人浏览 薄情痞子
摘要

这篇“Vue中的Computed与watch怎么用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue中的Computed与

这篇“Vue中的Computed与watch怎么用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue中的Computed与watch怎么用”文章吧。

computed

computed只接收一个getter函数

getter必须有返回值

computed返回一个只读响应式ref对象 (只读、响应式、对象)

注意:omputed只接收一个getter函数时,返回的只读对象,也就是不能修改他的返回值!

getter触发条件

  • computed返回值首次被读取时

  • getter绑定的响应式变量被修改时

<script setup>import { ref,computed } from 'vue'const num = ref(1)//computed返回一个只读响应式ref对象computedNum//computedNum是只读属性let computedNum = computed(() => num.value + 1)</script><template>    <p> num:{{ num }} </p>    <p>computedNum:{{ computedNum }}</p>    <!-- 修改响应式变量num 触发与之绑定的computed的getter-->      <button @click="num++">num++</button>    <!-- computedNum是只读属性-->      <button @click="computedNum++">computedNum++</button></template>

computed同时接收getter函数对象和setter函数对象

setter函数对象没有返回值

computed返回一个可读可写响应式对象

setter函数对象有参数,是getter的返回值,也是computed的值

修改computed返回值,触发setter函数对象执行,但不会真正修改computed返回值(setter内改变getter计算值就会改变computed返回值)

setter触发条件

computed返回值被修改时

实例:

<script setup>import { ref, computed } from 'vue'const num = ref(1)//getter(只读)let computedNum = computed(() => num.value + 1)//getter和setter (可读可写)let computedNum2 = computed({    get: () => num.value + 1,    set: (val) => {         console.log(val);        //setter中修改ref响应式变量num,将触发关联的num的getter计算        //computedNum和computedNum2的getter同时触发         num.value++          }})</script><template>    <p> num:{{ num }} </p>    <p>computedNum:{{ computedNum }}</p>    <p>computedNum2:{{ computedNum2 }}</p>    <button @click="num++">num++</button>    <!-- computedNum是只读属性,会有警告提醒 Write operation failed: computed value is readonly-->      <button @click="computedNum++">computedNum++</button>     <!-- computedNum2是可读可写属性-->      <button @click="computedNum2++">computedNum2++</button></template>

调试 Computed

使用范围:仅开发模式生效

computed的第二参数:带有 onTrackonTrigger 选项的对象

  • onTrack :getter关联的响应式数据时触发。

  • onTrigger :getter关联的响应式数据被修改时触发

<script setup>import { ref, computed } from 'vue'const num = ref(1)let computedNum = computed(() => num.value + 1, {    onTrack: (e) => {        console.log('onTrack');        console.log(e);    },    onTrigger: (e) => {        console.log('onTrigger');        console.log(e);    }})</script><template>    <p> num:{{ num }} </p>    <p>computedNum:{{ computedNum }}</p>    <!--每次 num++将触发onTrigger -->    <button @click="num++">num++</button></template>

watchEffect

语法:

  • 参数1:触发监听回调函数,回调函数可传入一个onInvalidate函数作为参数!

  • 可选参数2:对象,包含3个可选属性flush、onTrack、onTrigger

立即执行传入的一个函数,同时响应式追踪其依赖,并在其依赖变更时重新运行该函数。

会立即执行一次(和watch的immediate属性效果一致)

关联的响应式数据被修改时触发

会自动感知代码依赖,和watch不一样,watchEffect会主动绑定监听数据

局限性:不能监听对象(但可以监听对象的属性),只能监听类似ref基本数据类型的响应式数据

立即执行 监听基本数据类型

<script setup>import { ref, watchEffect } from 'vue'const num = ref(1)//会立即执行一次watchEffect(() => {    console.log('watchEffect');    num.value++})</script><template>    <p>num: {{ num }}</p>    <button @click="num++">num++</button></template>

停止watchEffect

隐式:组件卸载时自动停止

显式:调用watchEffect 返回值

const stop = watchEffect(() => {  })// 显式停止stop()

清理watchEffect

语法: watchEffect( onInvalidate=>{ onInvalidate(()=>{ }) })

onInvalidate 是一个函数!优先触发!

onInvalidate 执行时机:

watchEffect被重新触发时

组件卸载时

注意:关联的响应式数据首次被修改时不会触发onInvalidate函数!

作用: 清理定时器、事件监听removeEventListener 。。。

import { ref, watchEffect } from 'vue'const num = ref(1)watchEffect((onInvalidate ) => {    console.log('watchEffect-1');    num.value++    onInvalidate (()=>{        console.log('onInvalidate-2');    })    console.log('watchEffect-3');})//1、watchEffect   被重新触发时// onInvalidate-2// watchEffect-1// watchEffect-3//2、组件卸载时// onInvalidate-2//3、关联的响应式数据首次被修改(组件被挂载时)//  watchEffect-1//  watchEffect-3

watchPostEffect 和 watchSyncEffect

watchPostEffectwatchSyncEffectvue3.2新增,是watchEffect类似语法糖的东西,

watchEffect可选参数对象{ flush?: 'pre' | 'post' | 'sync'}中post和sync的语法糖,pre是默认值

推迟触发watchPostEffect

watchPostEffect 是watchEffect可选参数对象{flush:'post'}的语法糖

推迟watchEffect触发时机!组件更新前触发!也就是在生命周期onBeforeUpdateonUpdated之间触发

语法:

//推迟触发watchEffectwatchEffect(  () => {      },  {    flush: 'post'  })//Vue3.2语法糖watchPostEffectwatchPostEffect(()=>{  })

实例:

//实验watchEffect第二参数 flush: 'post'属性watchEffect(() => {    console.log("实验watchEffect第二参数 {flush: 'post'}属性");    console.log(obj.age);},{   flush:'post' })watchEffect(() => {    console.log("watchEffect正常时机触发");    console.log(obj.age);})//生命周期onUpdatedonUpdated(()=>{    console.log('onUpdated()');  })//生命周期onBeforeUpdateonBeforeUpdate(()=>{    console.log('onBeforeUpdate()');})

修改obj.age时,执行结果:

watchEffect正常时机触发
onBeforeUpdate()
实验watchEffect第二参数 {flush: 'post'}属性
onUpdated()

同步触发watchSyncEffect

watchSyncEffect 是watchEffect可选参数对象{flush:'sync'}的语法糖

强制效果始终同步触发!效率低!也就是默认watchEffect之前触发

语法:

watchEffect(  () => {    },  {    flush: 'sync'  })//Vue3.2语法糖watchSyncEffectwatchSyncEffect(()=>{  })

watchEffect不能监听对象

//假设修改了对象的属性值-修改了obj.ageconst obj = Reactive({ name: '小明', age: 18 })//watchEffect不能监听对象变化watchEffect(() => {    console.log('watchEffect监听对象变化');    console.log(obj);})//watchEffect可以监听对象属性变化watchEffect(() => {    console.log('watchEffect监听对象属性变化');    console.log(obj.age);})//watch监听对象变化watch(obj, (obj) => {    console.log('watch监听对象变化');    console.log(obj);})

总结:watchEffect用来监听能监听基本数据类型,不能监听对象,但能监听对象的属性;watch能监听基本数据类型和对象!

watch

语法:

  • 参数1-被监听数据(形式:单个数据、数组、带返回值的回调函数)

  • 参数2-触发监听的回调函数,无返回值

  • 可选参数3-对象{immediate: true,deep:true},对象含2个可选参数和Vue2参数效果一致

Vue3的watch和Vue2的watch是基本一样的

需要指定监听数据

惰性,只在被监听数据变化时才触发(immediate属性可以设置在初始化的时候触发)

监听单个数据

参数1被监听数据的形式:

单个基本数据类型;

回调函数:返回值为单个基本数据类型;

// 侦听一个 getter//被监听数据传入一个带返回值的回调函数const state = reactive({ count: 0 })watch(  () => state.count,  (count, prevCount) => {      })// 直接侦听一个 refconst count = ref(0)watch(count, (count, prevCount) => {  })

监听多个数据(传入数组)

watch([fooRef, barRef], ([foo, bar], [prevFoo, prevBar]) => {  })

官方文档总结

以下代码截取官方文档,从TS代码可以看出很多关于watch和watchEffect函数参数和返回值的细节!

computed

computed只接收一个getter函数

getter触发条件:

computed返回值首次被读取时

getter绑定的响应式变量被修改时

computed同时接收getter函数对象和setter函数对象

setter触发条件:computed返回值被修改时

// 只读的function computed<T>(  getter: () => T,  debuggerOptions?: DebuggerOptions): Readonly<Ref<Readonly<T>>>// 可写的function computed<T>(  options: {    get: () => T    set: (value: T) => void  },  debuggerOptions?: DebuggerOptions): Ref<T>interface DebuggerOptions {  onTrack?: (event: DebuggerEvent) => void  onTrigger?: (event: DebuggerEvent) => void}interface DebuggerEvent {  effect: ReactiveEffect  target: any  type: OperationTypes  key: string | symbol | undefined}

watchEffect

  • 参数1-触发监听回调函数,回调函数可传入一个onInvalidate函数作为参数!

  • 可选参数2-对象,包含3个可选属性flush、onTrack、onTrigger

function watchEffect(  effect: (onInvalidate: InvalidateCbReGIStrator) => void,  options?: WatchEffectOptions): StopHandleinterface WatchEffectOptions {  flush?: 'pre' | 'post' | 'sync' // 默认:'pre'  onTrack?: (event: DebuggerEvent) => void  onTrigger?: (event: DebuggerEvent) => void}interface DebuggerEvent {  effect: ReactiveEffect  target: any  type: OperationTypes  key: string | symbol | undefined}type InvalidateCbRegistrator = (invalidate: () => void) => voidtype StopHandle = () => void

watch

  • 参数1-被监听数据(形式:单个数据、数组、带返回值的回调函数)

  • 参数2-触发监听的回调函数,无返回值

  • 参数3-传入{immediate: true,deep:true}对象和Vue2参数效果一致

// 侦听单一源function watch<T>(  source: WatcherSource<T>,  callback: (    value: T,    oldValue: T,    onInvalidate: InvalidateCbRegistrator  ) => void,  options?: WatchOptions): StopHandle// 侦听多个源function watch<T extends WatcherSource<unknown>[]>(  sources: T  callback: (    values: MapSources<T>,    oldValues: MapSources<T>,    onInvalidate: InvalidateCbRegistrator  ) => void,  options? : WatchOptions): StopHandletype WatcherSource<T> = Ref<T> | (() => T)type MapSources<T> = {  [K in keyof T]: T[K] extends WatcherSource<infer V> ? V : never}// 参见 `watchEffect` 共享选项的类型声明interface WatchOptions extends WatchEffectOptions {  immediate?: boolean // 默认:false  deep?: boolean}

以上就是关于“Vue中的Computed与watch怎么用”这篇文章的内容,相信大家都有了一定的了解,希望小编分享的内容对大家有帮助,若想了解更多相关的知识内容,请关注编程网精选频道。

--结束END--

本文标题: Vue中的Computed与watch怎么用

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

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

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

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

下载Word文档
猜你喜欢
  • Vue中的Computed与watch怎么用
    这篇“Vue中的Computed与watch怎么用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“Vue中的Computed与...
    99+
    2023-06-30
  • Vue computed与watch怎么使用
    这篇文章主要讲解了“Vue computed与watch怎么使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Vue computed与watch怎么使用”吧!compu...
    99+
    2023-07-05
  • 详解Vue中Computed与watch的用法与区别
    目录computedcomputed只接收一个getter函数computed同时接收getter函数对象和setter函数对象调试 ComputedwatchEffect立即执行 ...
    99+
    2022-11-13
  • 怎么在vue中使用 Watch和Computed
    本篇文章给大家分享的是有关怎么在vue中使用 Watch和Computed,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。01. 监听器watch(1)作用watch:用于监听d...
    99+
    2023-06-15
  • Vue中的watch、watch和computed的区别是什么
    本文小编为大家详细介绍“Vue中的watch、watch和computed的区别是什么”,内容详细,步骤清晰,细节处理妥当,希望这篇“Vue中的watch、watch和computed的区别是什么”文章能帮助大家解决疑惑,下面跟着小编的思路...
    99+
    2023-07-02
  • Vue中Computed和Watch的作用是什么
    本篇文章给大家分享的是有关Vue中Computed和Watch的作用是什么,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。 Vue中的...
    99+
    2022-10-19
  • Vue中的watch是什么以及watch和computed的区别
    目录一、watch是什么?二、应用基本用法三、Watch和computed的区别computed和watch的综合运用实例需求:实现代码(helloworld.vue实现代码)一、w...
    99+
    2022-11-13
  • Vue3中的computed,watch,watchEffect怎么使用
    这篇“Vue3中的computed,watch,watchEffect怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“...
    99+
    2023-07-06
  • vue3中的watch和computed怎么使用
    2.监听 ref 数据2.1监听一个 ref 数据<template> <p>{{ age }}</p> <button @click="age++">click&...
    99+
    2023-05-21
    Vue3 watch computed
  • vue Watch和Computed的使用总结
    目录01. 监听器watch (1)作用 (2)属性和方法 (3)监听对象 (4)监听数组 02. 计算属性computed (1)计算属性的set方法 (2)区别 (3)使用场景 ...
    99+
    2022-11-12
  • Vue中的computed和watch用法及区别
    目录vue computed 是如何实现的computed demovue的watch是如何实现的watch demovue computed 是如何实现的 Vue 的 comput...
    99+
    2023-05-17
    Vue computed和watch Vue computed Vue watch
  • vue中computed 和 watch有什么区别
    vue中computed 和 watch有什么区别,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。一、computed 和 wa...
    99+
    2022-10-19
  • Vue中的computed和watch的区别是什么
    Vue中的computed和watch的区别是什么,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。下面带大家认识Vue中的computed 和 watch,介绍一下compu...
    99+
    2023-06-22
  • Vue中computed和watch的区别小结
    目录一、Vue中computed和watch的区别二、computed计算属性三、区别总结区别:计算属性computed支持缓存,只有依赖数据发生改变,才会重新进行计算;不支持异步,...
    99+
    2022-12-10
    Vue中computed和watch的区别 Vue中computed和watch异同
  • Vue3的Watch和computed怎么使用
    本篇内容介绍了“Vue3的Watch和computed怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成...
    99+
    2022-10-19
  • 分析 Vue 中的 computed 和 watch 的区别
    目录一、computed介绍1.1、get 和 set 用法 1.2、计算属性缓存二、watch介绍三、两者区别3.1、对于 computed3.2、对于 watch四、应...
    99+
    2022-11-12
  • Vue 中的 computed 和 watch 的区别详解
    目录computed注意应用场景watch总结computed computed 看上去是方法,但是实际上是计算属性,它会根据你所依赖的数据动态显示新的计算结果。计算结果会被缓存,c...
    99+
    2022-11-13
  • Vue中computed和watch的区别有哪些
    小编给大家分享一下Vue中computed和watch的区别有哪些,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!概述我们在 Vue 项目中多多少少都会有用到 co...
    99+
    2023-06-15
  • Vue中computed属性和watch,methods的区别
    目录computedwatchmethods归纳三者不同点1、methods2、computed3、watch在Vue中,computed、watch和methods是处理响应式数据...
    99+
    2023-05-19
    Vue computed属性 watch methods
  • vue中computed和watch的区别和运用场景
    这篇文章主要为大家展示了“vue中computed和watch的区别和运用场景”,内容简而易懂,条理清晰,希望能够帮助大家解决疑惑,下面让小编带领大家一起研究并学习一下“vue中computed和watch...
    99+
    2022-10-19
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作