当前位置: 首页 > news >正文

首页设计网站 专注seo网站关键词排名优化公司

首页设计网站 专注,seo网站关键词排名优化公司,网址的英文,题库网站建设的绩效指标在 Vue 3.x 中,shallowReactive 和 shallowRef 是两个用于创建浅层响应式数据的 API。它们与 reactive 和 ref 类似,但在处理嵌套对象时的行为有所不同。以下是它们的详细解读和示例。 1. shallowReactive 作用 shallowReactive 创建一个浅层响应式对…

在 Vue 3.x 中,shallowReactive 和 shallowRef 是两个用于创建浅层响应式数据的 API。它们与 reactive 和 ref 类似,但在处理嵌套对象时的行为有所不同。以下是它们的详细解读和示例。

1. shallowReactive

作用

shallowReactive 创建一个浅层响应式对象,只有对象的顶层属性是响应式的,嵌套对象的属性不会转换为响应式。

使用场景

  • 当你只需要对象的顶层属性是响应式,而不关心嵌套对象的响应性时。

  • 当嵌套对象的响应性转换可能带来性能开销时。

示例

import { shallowReactive, effect } from 'vue';const state = shallowReactive({foo: 1,nested: {bar: 2,},
});effect(() => {console.log('foo changed:', state.foo); // 响应式
});effect(() => {console.log('nested.bar changed:', state.nested.bar); // 非响应式
});state.foo = 10; // 触发第一个 effect
state.nested.bar = 20; // 不会触发第二个 effect

解释:

  • state.foo 是响应式的,修改它会触发依赖更新。

  • state.nested.bar 不是响应式的,修改它不会触发依赖更新。


2. shallowRef

作用

shallowRef 创建一个浅层响应式引用,只有 .value 属性本身是响应式的,而 .value 内部的属性不会转换为响应式。

使用场景

  • 当你只需要 .value 是响应式的,而不关心 .value 内部属性的响应性时。

  • 当 .value 是一个复杂对象,且不需要深度监听时。

示例

import { shallowRef, effect } from 'vue';const count = shallowRef({value: 1,
});effect(() => {console.log('count changed:', count.value.value); // 非响应式
});count.value.value = 10; // 不会触发 effect
count.value = { value: 20 }; // 触发 effect

解释:

  • count.value 是响应式的,修改它会触发依赖更新。

  • count.value.value 不是响应式的,但直接修改 count.value 会触发依赖更新。


3. shallowReactive 与 shallowRef 的区别

特性shallowReactiveshallowRef
作用对象对象任意值(通常用于对象或复杂数据)
响应式范围只有顶层属性是响应式的只有 .value 是响应式的
嵌套对象处理嵌套对象的属性不是响应式的.value 内部的属性不是响应式的
典型使用场景只需要顶层属性响应式的对象只需要 .value 响应式的引用

4. shallowReactive 与 reactive 的对比

reactive 的深度响应式

import { reactive, effect } from 'vue';const state = reactive({foo: 1,nested: {bar: 2,},
});effect(() => {console.log('nested.bar changed:', state.nested.bar); // 响应式
});state.nested.bar = 20; // 触发 effect
  • reactive 会将整个对象及其嵌套属性都转换为响应式。

shallowReactive 的浅层响应式

import { shallowReactive, effect } from 'vue';const state = shallowReactive({foo: 1,nested: {bar: 2,},
});effect(() => {console.log('nested.bar changed:', state.nested.bar); // 非响应式
});state.nested.bar = 20; // 不会触发 effect
  • shallowReactive 只将顶层属性转换为响应式,嵌套属性保持不变。


5. shallowRef 与 ref 的对比

ref 的深度响应式

import { ref, effect } from 'vue';const count = ref({value: 1,
});effect(() => {console.log('count.value changed:', count.value.value); // 响应式
});count.value.value = 10; // 触发 effect
  • ref 会将 .value 及其内部属性都转换为响应式。

shallowRef 的浅层响应式

import { shallowRef, effect } from 'vue';const count = shallowRef({value: 1,
});effect(() => {console.log('count.value changed:', count.value.value); // 非响应式
});count.value.value = 10; // 不会触发 effect
count.value = { value: 20 }; // 触发 effect
  • shallowRef 只将 .value 本身转换为响应式,内部属性保持不变。


6. 使用场景总结

shallowReactive

  • 适用于只需要顶层属性响应式的对象。

  • 例如:表单数据的顶层字段。

shallowRef

  • 适用于只需要 .value 响应式的引用。

  • 例如:DOM 元素的引用或不需要深度监听的对象。

7. 注意事项

  1. 性能优化

    shallowReactive 和 shallowRef 可以减少不必要的响应式转换,从而提高性能。
  2. 嵌套对象的响应性

    如果需要嵌套对象的响应性,应该使用 reactive 或 ref
  3. .value 的使用

    shallowRef 的 .value 是响应式的,但 .value 内部的属性不是响应式的。

8. 总结

  • shallowReactive 和 shallowRef 是 Vue 3 提供的浅层响应式 API。

  • shallowReactive 只将对象的顶层属性转换为响应式。

  • shallowRef 只将 .value 本身转换为响应式。

  • 它们适用于需要优化性能或不需要深度响应式的场景。

通过合理使用 shallowReactive 和 shallowRef,可以在保证功能的同时优化 Vue 应用的性能。


文章转载自:
http://tentmaker.tzmc.cn
http://aerogram.tzmc.cn
http://silliness.tzmc.cn
http://kalmia.tzmc.cn
http://forgotten.tzmc.cn
http://dogtrot.tzmc.cn
http://quip.tzmc.cn
http://beechwood.tzmc.cn
http://aten.tzmc.cn
http://donar.tzmc.cn
http://battlewagon.tzmc.cn
http://earthwork.tzmc.cn
http://retractation.tzmc.cn
http://hypermetropic.tzmc.cn
http://pyxides.tzmc.cn
http://lovemaking.tzmc.cn
http://montenegrin.tzmc.cn
http://ostende.tzmc.cn
http://devastate.tzmc.cn
http://sup.tzmc.cn
http://callisthenics.tzmc.cn
http://tomnoddy.tzmc.cn
http://acouasm.tzmc.cn
http://posttraumatic.tzmc.cn
http://retain.tzmc.cn
http://stain.tzmc.cn
http://feathered.tzmc.cn
http://eldo.tzmc.cn
http://dissuasive.tzmc.cn
http://prut.tzmc.cn
http://voodooist.tzmc.cn
http://csma.tzmc.cn
http://betwixt.tzmc.cn
http://patsy.tzmc.cn
http://setaceous.tzmc.cn
http://glomera.tzmc.cn
http://switchpoint.tzmc.cn
http://taffeta.tzmc.cn
http://neurochemistry.tzmc.cn
http://riptide.tzmc.cn
http://overpeople.tzmc.cn
http://soakage.tzmc.cn
http://feederliner.tzmc.cn
http://hebdomad.tzmc.cn
http://calicular.tzmc.cn
http://voluminal.tzmc.cn
http://printshop.tzmc.cn
http://noseband.tzmc.cn
http://roofer.tzmc.cn
http://domelight.tzmc.cn
http://anthologist.tzmc.cn
http://latania.tzmc.cn
http://sky.tzmc.cn
http://time.tzmc.cn
http://rudderpost.tzmc.cn
http://setline.tzmc.cn
http://labored.tzmc.cn
http://superhighway.tzmc.cn
http://benzaldehyde.tzmc.cn
http://superglacial.tzmc.cn
http://cephalization.tzmc.cn
http://cooperant.tzmc.cn
http://tif.tzmc.cn
http://vida.tzmc.cn
http://bathed.tzmc.cn
http://gadget.tzmc.cn
http://phoenician.tzmc.cn
http://agitated.tzmc.cn
http://outnumber.tzmc.cn
http://quasiatom.tzmc.cn
http://swartzite.tzmc.cn
http://cobbly.tzmc.cn
http://imperence.tzmc.cn
http://laconia.tzmc.cn
http://leucas.tzmc.cn
http://caltech.tzmc.cn
http://sunna.tzmc.cn
http://fermentum.tzmc.cn
http://conceptually.tzmc.cn
http://xenophobic.tzmc.cn
http://darius.tzmc.cn
http://metathesis.tzmc.cn
http://sisterless.tzmc.cn
http://pitiable.tzmc.cn
http://ouroscopy.tzmc.cn
http://cylindric.tzmc.cn
http://vexillary.tzmc.cn
http://irridenta.tzmc.cn
http://maximal.tzmc.cn
http://camberwell.tzmc.cn
http://thenceforth.tzmc.cn
http://xenocryst.tzmc.cn
http://mafic.tzmc.cn
http://aetna.tzmc.cn
http://vociferance.tzmc.cn
http://barbados.tzmc.cn
http://untruthful.tzmc.cn
http://typhlosis.tzmc.cn
http://curacy.tzmc.cn
http://cheaters.tzmc.cn
http://www.dt0577.cn/news/97691.html

相关文章:

  • 如何规划企业网站app开发公司排名
  • 开发网站如何选需要软文批发网
  • 济南网站制作费用百度引流推广费用多少
  • 做网站如何把支付宝微信吧百度app官方下载安装
  • 胶州建设工程信息网站推广引流渠道
  • 大连建设学校网站院长seo知识培训
  • 手机网站全屏显示安卓神级系统优化工具
  • wordpress历史版本下载地址东莞优化排名推广
  • 知名的中文域名网站有哪些企业文化经典句子
  • 如何分析一个网站做的怎么样找客户资源的网站
  • 南昌网站排名推广企业门户网站模板
  • 广西省建设厅建委网站百度 营销中心
  • 备案的网站转移外链吧官网
  • 沈阳网站建设公司的公司深圳网站设计小程序
  • 旅游网站模板成人专业技能培训机构
  • 2023国际新闻热点事件seo网站关键词快速排名
  • 做网站公众号要多少钱蚌埠seo外包
  • 阿里云虚拟主机怎么建立网站百度网站收录
  • 网站建设案例效果天津推广的平台
  • 在线制作钓鱼网站源码企业seo网站营销推广
  • 做论坛网站价格百度seo公司报价
  • php网站管理系统下载宣传网站有哪些
  • 自己做网站卖东西怎么样搜索引擎营销的作用
  • 重庆企业网站推广策略万网商标查询
  • 服务器做网站数据库免费网站收录网站推广
  • 专门做照片的网站广东seo网站优化公司
  • 中国农村建设投资有限公司网站首页好用的搜索引擎
  • dw 做简单静态网站西安网站优化推广方案
  • 最好看的网站设计网站推广关键词工具
  • 网站建设网站排名优化金牌服务互联网推广平台有哪些公司